php + mysql_num_rows 问题?

标签 php mysql

我有一个简单的 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/

相关文章:

php - 选择时将不存在的字段设置为值

php - 如何从 Datatables jQuery 插件中提取过滤后的数据?

javascript - Node JS 创建从图片到数据库的链接

java - JooQ 中的 DuplicateKeyException

html - 获取JSP中选中或选中的单选按钮值

php - Codeigniter 中的 JQuery 自动完成编码

php - Laravel 5.2 php artisan 迁移 :rollback error

php - 确保多维数组上的索引索引 mongodb 字段的正确方法是什么?

php - 更好地重写 3 个 mySQL 查询序列?

php - 模态窗口不关闭