[英]MySQL: Num Rows not working
I've stumbled upon a simple MySQL error, and it seems my attempts of fixing it are effortless. 我偶然发现了一个简单的MySQL错误,看来我修复它的尝试很轻松。 The problem, it's not counting.
问题是,这没有算在内。
Before I go further, I know that mysql_*
is Deprecated , and that I shouldn't use it. 在继续之前,我知道
mysql_*
已弃用 ,并且不应该使用它。 I should use mysqli_*
or PDO. 我应该使用
mysqli_*
或PDO。
This is my Query, and yes, the echo
is just for testing. 这是我的查询,是的,
echo
仅用于测试。
$ms_sql = mysql_query("SELECT * FROM mijnsp WHERE sp_username = '".$user['username']."'");
while ($mijnspusers = mysql_fetch_assoc($ms_sql)) {
$ms_count = mysql_num_rows($ms_sql);
if($ms_count <= 0){
echo "Result is empty";
}else{
echo $mijnspusers['new_username'];
}
I've tried to change the IF, but with no effect; 我试图更改IF,但是没有效果;
if($ms_count <= "0"){
or, like this 或者像这样
if($ms_count <= '0'){
Thank you in advance, Pascal 预先谢谢你,帕斯卡尔
Call 呼叫
$ms_count = mysql_num_rows($ms_sql);
before the while()
loop. 在
while()
循环之前。
You should do the mysql_num_rows()
before going into the loop. 进入循环之前,应先执行
mysql_num_rows()
。 If the num is 0, then you'll never run the loop to begin with. 如果num为0,那么您永远都不会运行循环。
Your code should be: 您的代码应为:
$ms_sql = mysql_query("SELECT * FROM mijnsp WHERE sp_username = '".$user['username']."'");
$ms_count = mysql_num_rows($ms_sql);
if($ms_count == 0){
echo "Result is empty";
}else{
while ($mijnspusers = mysql_fetch_assoc($ms_sql)) {
echo $mijnspusers['new_username'];
}
}`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.