我收到以下消息:
Notice: Undefined variable: dbh in /var/www/PDO/Functions/PDOFunctions.php on line 12 Fatal error: Call to a member function prepare() on a non-object in /var/www/PDO/Functions/PDOFunctions.php on line 12
$dbh = new PDO('mysql:host=localhost;dbname=pdo', 'root', 'xxxxxxxxxxx');
global $dbh;
function PDOFetch($Var)
{
$sth = $dbh->prepare("$Var"); //Error Line
$sth->execute();
$result = $sth->fetchAll();
return $result;
}
function PDONumb ($Var)
{
$Query = $dbh->prepare("{$Var}");
$Execute->execute();
$count = $Execute->rowCount();
return $count;
}
什麼是我的代碼的問題?
'global $ dbh'進入函數內部。 –
看看我的簡單[DByte庫](https://github.com/Xeoncross/DByte)。 – Xeoncross
問題是你不知道'global'是如何工作的。你應該用google搜索並閱讀文檔,然後再問Stack Overflow的問題...... – meagar