Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 结果从数据库中可用的精确行返回的值小于1(减1)_Php_Mysql_Database - Fatal编程技术网

Php 结果从数据库中可用的精确行返回的值小于1(减1)

Php 结果从数据库中可用的精确行返回的值小于1(减1),php,mysql,database,Php,Mysql,Database,您基本上是在切掉if语句中的第一个结果: $result = mysql_query($sql) or die(mysql_error()); echo "<table border='1'> <tr> <th>Asset ID</th> <th>Subasset Category</th> <th>Subasset Name</th> </tr>

您基本上是在切掉if语句中的第一个结果:

$result = mysql_query($sql) or die(mysql_error());

echo "<table border='1'>
    <tr>
    <th>Asset ID</th>
    <th>Subasset Category</th>
    <th>Subasset Name</th>
    </tr>";

while ($row = mysql_fetch_array($result)) {
    $subassetid = $row['subassetid'];
    $assetid = $row['assetid'];
    $subassetname = $row['subassetname'];
    $subassetcategory = $row['subassetcategory'];

    echo "<tr>";
    echo "<td>" . $assetid . "</td>";
    echo "<td>" . $subassetcategory . "</td>";
    echo "<td>" . $subassetname . "</td>";
    echo "<td>" . "<a href='viewassetdetail.php?subassetid=$subassetid&assetid=$assetid'>View<a/>" . "</td>";
    echo "</tr>";
}
echo "</table>";

mysql_close($connect);

?>
只需检查返回的行数即可保持结果集完整:

if ($row = mysql_fetch_array($result))

您必须使用
mysql\u fetch\u array()
检查是否有任何结果。您已经在那里消费了一行。所以,这就是您缺少的一行。您可以使用
mysql\u num\u rows()

来检查结果中是否有任何值。更简化的方法是在循环数组之前检查结果集。这样,如果它是空的,你就可以跳过它,直接进入空对话。

而不是

If ($result->num_rows() > 0 ) { do your stuff } else { no results found }
尝试使用以下方法:

if ($row = mysql_fetch_array($result))
{
  .....................
}

哦,太快了!!非常感谢!!我知道这一定很简单,但是的,直到现在我才明白。真的很感激!!和平!!=)好的,我已经从第一个答案(konsolenfreddy)中了解到了这一点,但是各位,非常感谢你们,非常感谢你们的回复!!现在我有点尴尬,因为这是很琐碎的事情,对吧?但看看有多少人在一瞬间回答=P@crystalz我很高兴能帮上忙。由于您是SO新手,您可能想阅读关于接受答案的内容。这将帮助其他有类似问题的人。
if(mysql_num_rows($result) > 0)
If ($result->num_rows() > 0 ) { do your stuff } else { no results found }
if ($row = mysql_fetch_array($result))
{
  .....................
}
 $num_rows = mysql_num_rows($result); //returns the number of rows selected

 if($num_rows==0)
 {
      //no data
 }
 else
 {
    //your code
 }