2
嘿傢伙我剛開始嘗試將我的查詢結構轉換爲PDO,並且遇到了一個奇怪的問題。當我在一個函數中調用一個pdo查詢連接,並且該連接被包含在該函數之外時,連接變得未定義。任何人都知道我在這裏做錯了什麼?我只是在玩它,我的例子如下。如何在功能上爲pdo與DB的通信定義連接
include("includes/connection.php");
function query(){
$user='user';
$id='100';
$sql = 'SELECT * FROM users';
$stmt = $conn->prepare($sql);
$result=$stmt->execute(array($user, $id));
// now iterate over the result as if we obtained
// the $stmt in a call to PDO::query()
while($r = $stmt->fetch(PDO::FETCH_ASSOC))
{
echo "$r[username] $r[id] \n";
}
}
query();
謝謝alix會做 – Scarface 2010-05-16 16:40:38