How can I show a message if no rows were allocated in MySQL?

My code is as follows:

$result = mysql_query("SELECT * FROM `picdb` WHERE `picid` = '$picid' ") or trigger_error(mysql_error()); 
while($row = mysql_fetch_array($result)){ 
foreach($row AS $key => $value) { $row[$key] = stripslashes($value); } 

The problem is that when there is no row to select, nothing happens, my $ row array is empty.

How can I present this "no row found" message if the rows do not match the selected?

+3
source share
5 answers

to try

if (mysql_num_rows($result) == 0) {
  // Show message
} else {
  // do your while stuff here
}
+17
source

Use mysql_num_rows()to determine the number of rows in your result.

$result = mysql_query("SELECT * FROM `picdb` WHERE `picid` = '$picid' ") or trigger_error(mysql_error()); 

if(mysql_num_rows($result) == 0) {
  echo "No row found!"
} else {
  while($row = mysql_fetch_array($result)) { 
    foreach($row AS $key => $value) {
      $row[$key] = stripslashes($value);
    }
  }
}
+8
source
if (mysql_num_rows($result) == 0) {
    echo "Result is empty!";
}

mysql_query, : http://php.net/mysql_query

+2

mysql_num_rows() . / , , num_rows .
mysql_fetch_array(). , .

<?php
$result = mysql_query("SELECT * FROM `picdb` WHERE `picid` = '$picid' ") or trigger_error(mysql_error()); 

$row = mysql_fetch_array($result, MYSQL_ASSOC);
if ( !$row ) {
  echo '0 rows';
}
else {
  do {
    echo htmlspecialchars($row['x']), "<br />\n";
  } while($row=mysql_fetch_array($result, MYSQL_ASSOC));
}

{} while(), $row = mysql_fetch... . .

+2

.

if(!$result = mysqli_query($con,"SELECT * FROM 'picdb' WHERE 'picid' = '$picid' ")){
// Enter you error message here.
} else {
// Do all your magic.
}
0

Source: https://habr.com/ru/post/1714700/


All Articles