简体   繁体   中英

PHP PDO fatal error while connecting MySQL database

I am getting following fatal error when i use PDO to connect and retrieve some output from MySQL

Fatal error: Call to a member function prepare() on a non-object in /home/ ... line 21

My PHP Code:

$dbConnection = new PDO('mysql:dbname=abc;host=127.0.0.1;charset=utf8', 'abc','abc');
$dbConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $pdo->prepare('SELECT current_date()'); //line 21
$stmt->execute();

change this line:

$stmt = $pdo->prepare('SELECT current_date()'); //line 21

To:

$stmt = $dbConnection->prepare('SELECT current_date()'); //line 21

Because $pdo is not defined here but $dbConnection

尝试将第四行更改为

$stmt= $dbConnection->prepare...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM