db_class.phpPHP的錯誤呼叫成員函數查詢()非對象上
<?php
class db_mysql
{
private $dbhost;
private $dbusername;
private $dbpassword;
private $db;
//Class is called with all parameters to make a successful connection.
//
function __construct($dbhost,$dbusername,$dbpassword,$db)
{
global $dbh;
try {
$dbh = new PDO("mysql:host=$dbhost;dbname=$db", $dbusername, $dbpassword);
foreach($dbh->query('show tables;') as $row) {
print_r($row);
}
//$dbh = null;
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
}
//Function to execute a query in the database and return result in a array
//
function db_mysql_query($queryst)
{
foreach($dbh->query($queryst) as $row) {
print_r($row);
}
}
}
的index.php:
<?php
include 'db_class.php';
$db_m = new db_mysql('localhost','root','','arsaas');
$db_m->db_mysql_query('show tables;');
?>
執行的index.php提供了以下錯誤: 通知:未定義的變量:在C:\ xampp \ htdocs \ srry \ db_class.php上的dbh在線32
致命錯誤:調用C:\ xampp \ htdocs \ srry中的非對象的成員函數query() \ db_class.php在線32
爲什麼說dbh是未定義的變量,當它在類的構造函數中實例化並聲明爲全局變量時?
任何幫助表示讚賞。提前致謝。
AR
特定的錯誤信息的哪一部分很難理解? – hakre 2012-07-08 16:20:12