How to list rows for a query or display 'no records' using a single query

前端 未结 3 1429
一个人的身影
一个人的身影 2021-01-26 10:45

How to list records for a query and to display \"no records\" when no rows returned using a single query?

Currently I am using a COUNT(*) query or using

相关标签:
3条回答
  • 2021-01-26 11:10

    Try Something like following

       if (mysql_num_rows ==  '0')
           'No Records'
        else
           //YOUE CODE HERE
    
    0 讨论(0)
  • 2021-01-26 11:14

    Important: I assume that the OP uses PHP as (s)he mentions mysql_num_rows. And I hope (s)he will tell me if I am wrong.


    It is your job in PHP to check whether the result is an empty set or not. I don't understand why you have to do another query. Maybe you have to clarify your question.

    Here a more complete example:

    $link = mysql_connect("localhost", "mysql_user", "mysql_password");
    mysql_select_db("database", $link);
    
    $result = mysql_query("SELECT * FROM table1", $link);
    
    // If if result set contains rows
    if(0 == mysql_num_rows($result)) {
        echo 'no records';
    }
    else { // Loop over the result set
        while(row = mysql_fetch_array($result)) {
           // do whatever you want with the data here
        }
    }
    

    Reference: mysql_num_rows, mysql_fetch_array


    Even if you don't use PHP, the approach is the same in other languages and there should be similar functions available.

    0 讨论(0)
  • 2021-01-26 11:15

    why do you need another query after mysql_num_rows?
    why not to just run your query and then check results with mysql_num_rows?

    0 讨论(0)
提交回复
热议问题