Welcome to WebmasterWorld Guest from 54.197.171.28

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Select first row when numrows > 1

   
12:44 pm on Nov 22, 2005 (gmt 0)

10+ Year Member



Hi

I have a table and some of the entries are duplicate entries. I want to modify this script so that if no rows are found, it does one thing, but if one or more rows are found, it just takes the first one.

if ($num_rows == 0){echo 'No row';}
if ($num_rows > 0){echo 'Row provided';}
if ($num_rows > 1){echo 'I will select your first row';}
{
while ($row = $connector->fetchArray($result)){
$q1=$row['q1'];
$q2=$row['q2'];
$q3=$row['q3'];
}

Any suggestiosn on how I can get it to retrieve the first row, when there are more than one rows?

Many thanks
Ed

12:57 pm on Nov 22, 2005 (gmt 0)

WebmasterWorld Senior Member dreamcatcher is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Hi Ed,

Use the LIMIT clause. ie: LIMIT 1

mysql_query("SELECT * FROM table LIMIT 1");

dc

1:30 pm on Nov 22, 2005 (gmt 0)

5+ Year Member



dreamcatcher is correct

often it is a lot easier to submit your query correctly at the database level

this prevents having to use PHP to create workarounds to solve your problem

4:39 pm on Nov 22, 2005 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



... and if the database you are using doesn't have some form of LIMIT keyword you can always break [php.net] your while [php.net] loop.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month