服务器-每次查询都得连接一次数据库,否则不继续执行的问题。
程序员文章站
2022-05-13 10:34:18
...
服务器mysqlphp数据库linux
//连接数据库$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'];}
请问问题出在哪里?如何才能像本机上测试那样一次连接多次查询?