hiçbir satır mysql php seçildiğinde mesaj göstermek

4 Cevap php

Benim kod şöyle görünür:

$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); }

Sorun benim $ satır dizi boştur, seçmek için hiçbir satır olduğunda, hiçbir şey olmuyor yani.

Nasıl bu mevcut hiçbir satır seçme maç ne zaman "bulunamadı satırın" mesajı olabilir?

4 Cevap

denemek

if (mysql_num_rows($result) == 0) {
  // Show message
} else {
  // do your while stuff here
}
if (mysql_num_rows($result) == 0) {
    echo "Result is empty!";
}

Sen açıklamasını okumak vermeliydim mysql_query, size cevap verir: http://php.net/mysql_query

mysql_num_rows() only works on buffered queries. If you have potentially a large/huge result set you might switch to unbuffered queries and num_rows is not an option anymore.
You can test the return value of the first call to mysql_fetch_array(). If it's FALSE there were no matching records.

<?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));
}

Bu do {} while () olumsuz $ satır = mysql_fetch ... kod çoğaltılmış olmasıdır. Ama sadece küçük bir ikili var.