我有一个简单的 sql 语句,我想根据返回的行数执行不同的操作。
$result_lists = mysql_num_rows(mysql_query("SELECT * FROM db_table"));
//To see the number returned
print_r($result_lists);
switch($result_lists) {
case($result_lists == 0):
//To prove which option is actually happening
print_r('lists==0: '.$result_lists);
break;
case($result_lists > 1):
//To prove which option is actually happening
print_r('lists>1: '.$result_lists);
break;
case($result_lists == 1):
//To prove which option is actually happening
print_r('lists==1: '.$result_lists);
break;
}
如果找到 1 行或多行,则使用正确的大小写,但是,如果返回零行,则出于某种原因执行 (> 1) 大小写。
谁能看出哪里出了问题?
感谢任何建议。
谢谢。
最佳答案
您在滥用 switch 语句 - 您应该将其替换为 if
或像这样更改它:
switch ($result_lists)
{
case 0:
//To prove which option is actually happening
print_r('lists==0: '.$result_lists);
break;
case 1:
//To prove which option is actually happening
print_r('lists==1: '.$result_lists);
break;
default:
//To prove which option is actually happening
print_r('lists>1: '.$result_lists);
break;
}
你现在做的是这样的:
case($result_lists == 0):
// is like doing
if ($result_lists == ($result_lists == 0))
// which when $result_lists = 0 is the same as
if ($result_lists == true)
if (0 == 1)
// therefore false so it drops through to the next statement
case($result_lists > 1)
// the same as
if ($result_lists == ($result_lists > 1))
// when $result_lists = 0:
if ($result_lists == false)
if (0 == 0)
关于php + mysql_num_rows 问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1459540/