[ACCEPTED]-PHP Check MySQL Last Row-mysql
Accepted answer
You can use mysql_num_rows()
prior to your while
loop, and then 1 use that value for your condition:
$numResults = mysql_num_rows($result);
$counter = 0
while ($row = mysql_fetch_array($result)) {
if (++$counter == $numResults) {
// last row
} else {
// not last row
}
}
$result = mysql_query("SELECT *SOMETHING* ");
$i = 1;
$allRows = mysql_num_rows($result);
while($row = mysql_fetch_array($result)){
if ($allRows == $i) {
/* Do Something Here*/
} else {
/* Do Another Thing Here*/}
}
$i++;
}
but please take in consideration 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++;
}
0
$allRows = $stmt->rowCount();
Didn't work for me, had to use:
$numResults = $result->num_rows;
0
Try this:
$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';
}
}
0
Try having a counter inside the while loop 1 and then checking it against mysql_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";
}
}
0
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.