這個問題在這里已經有了答案: 為什么這個 PDO 陳述句會默默地失敗? (1 個回答) 6 小時前關閉。
我剛剛開始通過 youtube 上的視頻了解 PHP OOP,并且我正在構建一個網站應用程式。
正如那個人解釋的那樣,我必須使用 PDO 連接到 SQL 資料庫。我遵循了他所做的一切,但我對 PDO 的執行部分不起作用,我不知道它有什么問題。代碼如下。它不會帶來任何錯誤它只是不會從資料庫中帶回任何資料。我在函式之間添加了一些回聲,以了解代碼的運行情況,這就是我發現它準備查詢但從不執行它的方式。請幫我解決這個問題。謝謝你。
class Database{
private function connect()
{
$string = DBDRIVER . ":host =" .DBHOST.";dbname =".DBNAME;
if ($conn = new PDO($string, DBUSER, DBPASS)) {
return $conn;
} else {
die("Could not connect to database");
}
}
上面的代碼是與資料庫的連接。據我所知,它作業正常。
下面的代碼應該將資料庫中的資料回傳以使用 view.php 檔案顯示
public function query($query, $data = array(), $data_type = "object")
{
$con = $this->connect();
$stm = $con->prepare($query);
print_r($stm);
if ($stm) {
$check = $stm->execute($data); //right here is where I concluded that the code stops
print_r($check);//does not print anything
if ($check) {
if ($data_type == "object") {
$data = $stm->fetchAll(PDO::FETCH_OBJ);
} else {
$data = $stm->fetchAll(PDO::FETCH_ASSOC);
}
if (is_array($data) && count($data) > 0) {
return $data;
}
}
}
return false;
}
下面是撰寫在控制器中的部分,用于將查詢分配給資料庫執行。
function index(){
$db = new Database();
$data = $db->query("select * from users");
if($data){echo $data;} //echos nothing because $data is empty
$this -> view('home', ['rows' => $data]);
}
uj5u.com熱心網友回復:
我已經測驗了您的代碼發生錯誤是因為您在 dbname 和 = 之間有一個空格。DBNAME 后面的分號也缺失(不需要)。
class Database{
private function connect()
{
$string = DBDRIVER . ":host=" .DBHOST.";dbname=".DBNAME.";";
if ($conn = new PDO($string, DBUSER, DBPASS)) {
return $conn;
} else {
die("Could not connect to database");
}
}
現在它對我有用。
我用這個連接類很久了,也許你想用它。
class Connection{
private const CONNECTION = [
"Host" => "",
"User" => "",
"Password" => "",
"Databasename" => ""
];
/**
* Includes the pdo connection.
* @var \PDO|null
*/
private \PDO|null $PDO = null;
/**
* Includes the already called instance.
* @var Connection|null
*/
private static self|null $Connection = null;
/**
* Creates the DB connection and sets it to the class.
* @return void
* @throws Exception
*/
private function __construct()
{
$Connection_str = 'mysql:';
$Connection_str .= 'host=' . self::CONNECTION["Host"] . ';';
$Connection_str .= 'dbname=' . self::CONNECTION["Databasename"] . ';';
$Connection_str .= 'charset=utf8mb4';
$this->PDO = new \PDO($Connection_str, self::CONNECTION["User"], self::CONNECTION["Password"]);
$this->PDO->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$this->PDO->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
$this->PDO->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
}
/**
* Returns the static DB connection instance.
* @return Connection
* @throws Exception
*/
public static function get(): Connection
{
return !isset(self::$Connection) ? self::$Connection = new self : self::$Connection;
}
/**
* Returns the PDO instance.
* @return PDO
* @throws Exception
*/
public function getPDO(): \PDO
{
return $this->PDO;
}
}
您可以使用以下代碼使用連接類:
$SQLString = "select * from users";
$Data = [
];
$Query = Connection::get()->getPDO()->prepare($SQLString);
$Query->execute($Data);
$Data = $Query->fetchAll();
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/486259.html
下一篇:為同一屬性創建兩個單獨的驗證訊息
