Reputation: 9
i have a fetch row query that returns
10 9 8 7 6 5 4 3 2 1
however, i only want to return the last value, which is "1" in this case.
i tried to do echo row[0][9]
but it doesn't work.
how do i get the last value?
Upvotes: 0
Views: 9189
Reputation: 11
Here's my code.
$query="SELECT blog_id FROM myblogs_view where blog_id<'$id' ORDER BY blog_id DESC LIMIT 10";
$result=mysql_query($query);
while($row=mysql_fetch_row($result) or die(mysql_error())) {
echo $row;
}
This returns the values in one row. how do i access the value in the last row only?
Upvotes: 0
Reputation: 9874
You only want the 10th last row that was added to the table? That would be the 10th id in the list if you sort by id descending. If that's what you want, you also could tell MySQL to return exactly that row. The query gets a bit more complicated though:
SELECT t1.*
FROM table t1
INNER JOIN (
SELECT id
FROM table
ORDER BY id DESC
LIMIT 10
) t2 ON t1.id = t2.id
ORDER BY t1.id ASC
LIMIT 1
Upvotes: 0
Reputation: 31910
I'd do it all on MySQL side, so you don't split the logic between PHP and MySQL too much (if that's applicable in this case) and use query with LIMIT 1
and ORDER BY DESC
(or ASC, depending on your table).
Upvotes: 0
Reputation: 32148
you can use the end()
function
$row = array(10, 9, 8, 7);
echo end($row); // displays 7
Upvotes: 1
Reputation: 27214
Try echo explode(' ', $row[0])[9]
Been a while since I've done PHP though, YMMV.
Upvotes: 0
Reputation: 19862
<?php
$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);
echo $row[0]; // 42
echo $row[1]; // the email value
?>
Take a look @ this example. Copied from here. You should be able to achieve what you wanted by taking a look at the above example.
If you know what the column index is, you are good to go. But if you ALWAYS want the last column take a look at the mysql_num_fields function.
Upvotes: 0