Jump to content

select last row


matttherat

Recommended Posts

hi. just started a website talkietaco.com and at the momment my code selects the first row and displays it. this is all well and good but when i add a new row to the table it gos to the bottom. I want to be able to select the last row and echo it out. Any ideas? would i need to add an id row or something?

Heres the code and thank you in advance for any help people can offer.

 

<?php


$con = mysql_connect("localhost","","");



if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  
mysql_select_db("mainbase", $con);


$result = mysql_query("SELECT * FROM matteroffact");

echo "<table border='0'>
<tr>
<th></th>
</tr>";

$row = mysql_fetch_array($result) or die(mysql_error());
echo "<tr>";
echo "<td><strong>" . $row['question']. "</strong>  ". $row['answer']; "</td>";
echo "<tr>";

echo "</table>";


?>

Link to comment
Share on other sites

Dedicated concatenation is sooooo yesterday, use braces instead itll clean up your code and it can handle arrays

 

<?php

$con = mysql_connect("localhost","","");

if (!$con){
  die('Could not connect: ' . mysql_error());
  }
  
mysql_select_db("mainbase", $con);

$result = mysql_query("SELECT * FROM matteroffact");

echo "<table border='0'>
<tr>
<th></th>
</tr>";

$row = mysql_fetch_array($result) or die(mysql_error());
echo "<tr><td>
          <strong>{$row['question']}</strong> {$row['answer']}
          </td><tr>
          </table>";

?>

Link to comment
Share on other sites

also if you're looking for the last inserted id I would use this

select last_insert_id() as lastid from table limit 1 

 

that actually gets the id of the last row inserted by the current connection. This saves you headaches further down the road if you get more than one connection putting data into your database.

 

Even better:

 

mysql_query("bla bla");
$id = mysql_insert_id();

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.