我最近对我正在处理的网站进行了大量更改,并通过整个项目替换了与数据库交互的旧mysql_方法。在这个特定的脚本中我很难让它工作相同。
旧代码是
$checkinfo = mysql_query("SELECT * FROM `myusers` WHERE `userid` = '$uid' LIMIT 1") or die(mysql_error());
if(mysql_num_rows($checkinfo) < 1){ //log and die if user isnt in db
die("Incident has been logged!"); }
$myinfo = mysql_fetch_assoc($checkinfo);
我的新代码是
$checkinfo = $mysqli->query("SELECT * FROM `myusers` WHERE `userid` = '$uid' LIMIT 1") or die('Error : ('. $mysqli->errno .') '. $mysqli->error);
if($checkinfo->fetch_row() < 1){
die("Incident has been logged!"); }
$myinfo = $checkinfo->fetch_assoc();
现在它根本没有为其余的代码设置我的数组...请指出我的愚蠢!感谢
答案 0 :(得分:3)
通过使用->fetch_row()
,它已经输入了第一行。由于您明确设置了LIMIT 1
,因此下一次提取调用导致NULL
。
将其更改为->num_rows
:
$checkinfo = $mysqli->query("SELECT * FROM `myusers` WHERE `userid` = '$uid' LIMIT 1") or die('Error : ('. $mysqli->errno .') '. $mysqli->error);
if($checkinfo->num_rows < 1){
die("Incident has been logged!"); // change this to something more meaningful.
}
$myinfo = $checkinfo->fetch_assoc();