Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 11th, 2004, 07:40 PM
Authorized User
 
Join Date: Aug 2003
Location: Coeur d\'Alene, ID, USA.
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Tachyon
Default getting a specific row from a MySQL result set

I'm having trouble getting my program to select a specific row from a MySQL result set. I know how to fetch the first row from a result set and I know how to print out the entire result set but that's about all I can do with it.

My situation is this: the program runs a query and grabs all the rows from a table (skill_info) where Joe has a listed skill. The result set has 3 rows and one of those rows is Joe's skill in carpentry. I want to grab that row and put it into $row[].

I thought of trying this:
Code:
mysql_data_seek ($result, 0);
    while ($row = mysql_fetch_array($result)){
    extract($row);
        if ($row['skillnum'] == "54"){
        exit;
        }
    }

but it exits the whole script/program when I only want to exit the loop.

Can anyone help? There must be a way to make mysql_fetch_array fetch only the row I want.

__________________
An overworked Web Developer who\'s expected to know everything yet given time to study nothing.
Reply With Quote
  #2 (permalink)  
Old July 12th, 2004, 05:07 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
Default

A number of point sripng to mind.

a) To exit a loop, use "break".

b) the resultset is just a PHP array, so you can treat it as such by using array-handling functions like in_array() or array_search(), and so on.

c) Why don't you add a test for "WHERE skillnum='54'" in you original query :)?

Take it easy,
Dan
Reply With Quote
  #3 (permalink)  
Old July 12th, 2004, 09:39 AM
Authorized User
 
Join Date: Aug 2003
Location: Coeur d\'Alene, ID, USA.
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Tachyon
Default

Thanks for the info. I know what to try out now.

My situation is this: I've got a page that displays information on 7 different skills. Rather than query the database 7 times I want to do it once and use the result set 7 times.

Reply With Quote
  #4 (permalink)  
Old July 12th, 2004, 11:55 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes, that certainly makes sense, and it's something I do a lot of - especially when the results come form massive correlated subqueries. Like I say, you can handle the resultset as what it is: an associative array (at least, it's associative, if that's how you have fetch_array set to work, which from my reading you code abovce, I take it it is).

Take it easy,
Dan
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Decode function to display result in same row yogeshyl SQL Language 0 July 2nd, 2008 12:40 AM
Pick up specific row from database Lofa Dreamweaver (all versions) 2 February 17th, 2008 01:12 AM
Get specific row from database Lofa ASP.NET 1.0 and 1.1 Basics 9 July 11th, 2007 01:17 PM
Insert row between specific rows Paula222 Access VBA 2 February 10th, 2006 03:56 PM
datagrids question -- finding a specific row bazookajoe76 C# 1 January 22nd, 2005 12:08 PM



All times are GMT -4. The time now is 05:19 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.