Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.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检查MySQL最后一行_Php_Mysql - Fatal编程技术网

PHP检查MySQL最后一行

PHP检查MySQL最后一行,php,mysql,Php,Mysql,我有一个关于PHP的简单问题来检查这是否是MySQL的最后一行。例如,我有以下代码: $result = mysql_query("SELECT *SOMETHING* "); while($row = mysql_fetch_array($result)) { if (*this is the last row*) {/* Do Something Here*/} else {/* Do Another Thing Here*/} } 我很难确定这一排是否是最后一排。知道怎么做吗?谢谢。您

我有一个关于PHP的简单问题来检查这是否是MySQL的最后一行。例如,我有以下代码:

$result = mysql_query("SELECT *SOMETHING* ");

while($row = mysql_fetch_array($result))
{
if (*this is the last row*)
{/* Do Something Here*/}
else
{/* Do Another Thing Here*/}
}
我很难确定这一排是否是最后一排。知道怎么做吗?谢谢。

您可以在
while
循环之前使用,然后根据您的情况使用该值:

$numResults = mysql_num_rows($result);
$counter = 0
while ($row = mysql_fetch_array($result)) {
    if (++$counter == $numResults) {
        // last row
    } else {
        // not last row
    }
}
但请考虑PDO

$db = new PDO('mysql:host=localhost;dbname=testdb', 'username', 'password');
$stmt = $db->query("SELECT * FROM table");
$allRows = $stmt->rowCount();
$i = 1;
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

    if ($allRows == $i) {
        /* Do Something Here*/
    } else {
        /* Do Another Thing Here*/}
    }
    $i++;
}

尝试在while循环中使用一个计数器,然后对照mysql\u num\u rows()检查它。

尝试以下操作:

$result = mysql_query("SELECT colum_name, COUNT(*) AS `count` FROM table");

$i = 0;

while($row = mysql_fetch_assoc($result))
{
    $i++;

    if($i == $row['count'])
    {
        echo 'last row';
    }
    else
    {
        echo 'not last row';
    }
}
不适合我,必须使用:

$numResults = $result->num_rows;

请不要对新代码使用
mysql.*
函数。它们不再得到维护,社区已开始恢复。看到了吗?相反,你应该学习并使用或。如果你不能决定,将帮助你做出选择。如果你想学习。
$allRows = $stmt->rowCount();
$numResults = $result->num_rows;
$result = //array from the result of sql query.
$key = 1000; 
$row_count = count($result); 
if($key)
{
    if($key == $row_count-1)  //array start from 0, so we need to subtract.
    {
       echo "Last row";             
    }
    else
    {
       echo "Still rows are there";             
    }
}