Forum Moderators: coopster

Message Too Old, No Replies

sql query question

         

bysonary

11:28 pm on Feb 20, 2007 (gmt 0)

10+ Year Member



hello, I have a question relating to sql and php

I have a table in a database, the first entry in the table might have an id of 4 or and id of 6 depending on what has been added/deleted.

is there a way to select only the first record in the database and determine the id?

sort of like

SELECT id FROM table ORDER BY id ASC LIMIT 1

then something like $id = $row['id'];

can anyone confirm if this is the correct way to go about it or if there is a better way?

Psychopsia

11:31 pm on Feb 20, 2007 (gmt 0)

10+ Year Member



use WHERE;

"SELECT id FROM table WHERE id = " . $id

bysonary

11:51 pm on Feb 20, 2007 (gmt 0)

10+ Year Member



thats all ok i understand that using where will select the id where it is equal to $id, but what if i dont know the value of $id

i need to select the first row from the database and only the first row and extract the id from that row and then store it in Sid

bysonary

12:05 am on Feb 21, 2007 (gmt 0)

10+ Year Member



The below code does what i want, i think, i haven't fully tested it but it seems to work fine for now.


<?php
include '/home/www/juttuffi/dbc.php';

$query = mysql_query("SELECT qid FROM tquestions ORDER BY qid ASC LIMIT 1") or die(mysql_error());
$row = mysql_fetch_array($query,MYSQL_ASSOC);

$id = $row['qid'];

if(isset($id))
{
echo $id;
}
else
{
echo "wrong";
}

mysql_close($dbc);
?>