homepage Welcome to WebmasterWorld Guest from 54.237.78.165
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Select first row when numrows > 1
s9901470

5+ Year Member



 
Msg#: 10888 posted 12:44 pm on Nov 22, 2005 (gmt 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

 

dreamcatcher

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



 
Msg#: 10888 posted 12:57 pm on Nov 22, 2005 (gmt 0)

Hi Ed,

Use the LIMIT clause. ie: LIMIT 1

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

dc

DrDel

5+ Year Member



 
Msg#: 10888 posted 1:30 pm on Nov 22, 2005 (gmt 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

coopster

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



 
Msg#: 10888 posted 4:39 pm on Nov 22, 2005 (gmt 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved