Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 11th, 2004, 07:40 PM
Authorized User
 
Join Date: Aug 2003
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.
 
Old July 12th, 2004, 05:07 AM
Friend of Wrox
 
Join Date: Jun 2003
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
 
Old July 12th, 2004, 09:39 AM
Authorized User
 
Join Date: Aug 2003
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.

 
Old July 12th, 2004, 11:55 AM
Friend of Wrox
 
Join Date: Jun 2003
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





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





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