//連接數據庫
$pdo = new PDO("mysql:host=$db_host; dbname=$db_name", $db_username, $db_password);
$pdo->query('SET NAMES utf8');
//導入活動信息
$sql = "SELECT * FROM activity WHERE a_id = '$activity_id'";
$rs = $pdo->query($sql);
while($row = $rs->fetch()){
$activity_uno=$row['u_no'];
$activity_name=$row['a_name'];
}
//導入用戶信息
$sql = "SELECT * FROM user WHERE No = '$activity_uno'";
$rs = $pdo->query($sql);
while($row = $rs->fetch()){
$activity_usernick=$row['Nickname'];
$activity_uservtype=$row['vtype'];
$activity_userverify=$row['verify'];
}
在本機上(WAMP環境)這樣寫的代碼,運行結果是正常的,一次連接就可以多次查詢。但是傳到服務器上(LAMP),每次查詢前都要連接一次數據庫,否則就不會繼續查詢。
在服務器上改成這樣才能正常:
//連接數據庫
$pdo = new PDO("mysql:host=$db_host; dbname=$db_name", $db_username, $db_password);
$pdo->query('SET NAMES utf8');
//導入活動信息
$sql = "SELECT * FROM activity WHERE a_id = '$activity_id'";
$rs = $pdo->query($sql);
while($row = $rs->fetch()){
$activity_uno=$row['u_no'];
$activity_name=$row['a_name'];
}
//連接數據庫
$pdo = new PDO("mysql:host=$db_host; dbname=$db_name", $db_username, $db_password);
$pdo->query('SET NAMES utf8');
//導入用戶信息
$sql = "SELECT * FROM user WHERE No = '$activity_uno'";
$rs = $pdo->query($sql);
while($row = $rs->fetch()){
$activity_usernick=$row['Nickname'];
$activity_uservtype=$row['vtype'];
$activity_userverify=$row['verify'];
}
請問問題出在哪裡?如何才能像本機上測試那樣一次連接多次查詢?
服務器(LAMP)建立一個連接池