当我使用PDO连接并从MySQL检索某些输出
时,我遇到致命错误致命错误:在非对象中调用成员函数prepare() / home / ...第21行
我的PHP代码:
$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();
答案 0 :(得分:3)
更改此行:
$stmt = $pdo->prepare('SELECT current_date()'); //line 21
要:
$stmt = $dbConnection->prepare('SELECT current_date()'); //line 21
由于此处未定义$pdo
,$dbConnection
答案 1 :(得分:1)
尝试将第4行更改为
$stmt= $dbConnection->prepare...