Welcome to WebmasterWorld Guest from 54.167.111.180

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)

Junior Member

10+ Year Member

joined:Jan 28, 2005
posts:97
votes: 0


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)

Senior Member

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

joined:Mar 30, 2003
posts:3719
votes: 0


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)

New User

10+ Year Member

joined:Nov 12, 2005
posts:10
votes: 0


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)

Administrator

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

joined:July 31, 2003
posts:12537
votes: 0


... 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.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members