此代码出错。
致命错误:在第12行的C:\ wamp \ www \ progapps \ addReminder.php中的非对象上调用成员函数prepare()
这是我的代码:
addReminder.php
<?php
include_once("includes/database.php");
try {
global $dbh;
$query = $dbh -> prepare("SELECT * FROM reminder_type;");
$query->setFetchMode(PDO::FETCH_ASSOC);
$query ->execute();
} catch(PDOException $ex) {
echo $ex->getMessage();
}
?>
这是数据库的连接。
database.php中
<?php
include("constants.php");
class MySQLDB {
function MySQLDB(){
global $dbh;
try{
$dbh = new PDO('mysql:host='.DB_SERVER.';dbname='.DB_NAME.'',DB_USER,DB_PASS);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
}catch(PDOException $e){
echo $e->getMessage();
die();
}
}
}
以下是常量
constants.php
<?php
$currency = '₱';
define("DB_SERVER", "localhost");
define("DB_USER", "root");
define("DB_PASS", "");
define("DB_NAME", "hrisdb");
?>
可能的原因是什么?我该如何解决这个问题?谢谢你们,伙计们!
答案 0 :(得分:1)
这应该有效
$currency = '₱';
define("DB_SERVER", "localhost");
define("DB_USER", "root");
define("DB_PASS", "");
define("DB_NAME", "hrisdb");
try {
$dbh = new PDO('mysql:host=' . DB_SERVER . ';dbname=' . DB_NAME . '', DB_USER, DB_PASS);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
} catch (PDOException $e) {
echo $e->getMessage();
die();
}
try {
$query = $dbh->prepare("SELECT * FROM reminder_type;");
$query->setFetchMode(PDO::FETCH_ASSOC);
$query->execute();
} catch (PDOException $ex) {
echo $ex->getMessage();
}
如果你想在课堂上看一下这个extending PDO class
答案 1 :(得分:0)
您还没有调用函数MySQLDB
。因此变量$dbh
甚至不存在。