Forum Moderators: coopster

Message Too Old, No Replies

Acessing the last row of a table only

Acessing the last row of a table only

         

Imy_S3

4:37 am on Mar 21, 2004 (gmt 0)

10+ Year Member



Hi

I got some code below.
The database consists of about 100 rows.

How do i do it so i only output the values of the last row in the database i.e only the 100th line

Hope you can help
Thanks in advance

$con = pg_connect("host=dbteach port=5 dbname=u user=u password=p");
$sql = "select * from userDetails";
$r = pg_exec($con, $sql);
$nrowss = pg_numrows($r);

if($nrowss!= 0)
{
for($j=0; $j<$nrowss; $j++)
{
$row = pg_fetch_array ($r);
echo 'Please keep the following for reference <br>';
echo "Your reference Number <INPUT TYPE=\"text\" NAME=\"uid\" VALUE=\"" .$row["userid"]. "\"> <br>";
echo "Your Email address is <br><INPUT TYPE=\"text\" NAME=\"uemail\" VALUE=\"" .$row["useremail"]. "\"> <br>";

}
}

Gorilla

5:09 am on Mar 21, 2004 (gmt 0)

10+ Year Member



The way to solve this problem is by finding the exact row/record you want to show by way of the SQL Select statement. You should ad a WHERE clause to select only the record of interest. Something like this: "SELECT * FROM userDetails WHERE userid='this users userid'".

dreamcatcher

7:54 am on Mar 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$sql = "select * from userDetails DESC LIMIT 1";

Also if you are only pulling one row, you won`t need a for loop.


if($nrowss!= 0)
{
$row = pg_fetch_array ($r);
echo 'Please keep the following for reference <br>';
echo "Your reference Number <INPUT TYPE=\"text\" NAME=\"uid\" VALUE=\"" .$row["userid"]. "\"> <br>";
echo "Your Email address is <br><INPUT TYPE=\"text\" NAME=\"uemail\" VALUE=\"" .$row["useremail"]. "\"> <br>";

}