homepage Welcome to WebmasterWorld Guest from 54.198.140.182
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
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




msg:1245457
 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




msg:1245458
 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




msg:1245459
 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




msg:1245460
 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