Jump to content

php ~ select from database help


jennatar

Recommended Posts

Hiya!

 

So, I'm beating my head against the wall on this simple little thing. I don't understand why it won't work, so I'd appreciate it if someone could toss me a bone. I'm "self-taught", meaning it's probably something really obvious.

 

I'm trying to query a second table "while" an initial query is in progress.

 

I've echo'ed at various points to see what's going wrong; $gid returns a value, so the first query is working fine. I then use a second query to search another table based on the value of $gid. This query turns up a null result (zero echos), even though the table has a row that matches $gid.

 

Code:

$result = mysql_query("SELECT * FROM groupuser WHERE user='$username'");

while ($row = mysql_fetch_array($result)) {

$gid = $row['gid'];

echo "GIDs is " . $gid . ".<br />";

$gresult = mysql_query("SELECT * FROM group where gid='$gid'");

while ($grow = mysql_fetch_array($gresult)) {

echo $grow['name'];

}

}

 

The second query is set up exactly like the first one, so I don't understand why it won't work. In regards to the table 'group', it does contain both columns for 'gid' and 'name'. None of the fields are empty. 'gid' is the primary key for the table, set on auto_increment. Both tables are within the same database.

 

So... can you not use a query within a query? If that's the case, how would you go about doing a query based upon a return from an earlier query?

 

Or... is it that I can't query the auto_increment...? (I doubt it's this, but... I have no idea.)

 

Help, anyone? Thanks!

 

 

Link to comment
Share on other sites

1. You could probably do it in one query instead of two.

 

2. Put some debugging code into your script, that will display mysql error messages. These messages will help in finding errors.

 

Example:

 

$result = mysql_query("SELECT * FROM groupuser WHERE user='$username'") or die(mysql_error());

 

(do the same for the second query)

 

 

Link to comment
Share on other sites

Hey ...

 

 

u r making two nested fetch ... I think this is the problem ...

I didn't use mysql b4 , I use Oracle database but there I can't make nested fetch ...

fetch after another

 

Solution1:

$result = mysql_query("SELECT * FROM groupuser WHERE user='$username'");

      while ($row = mysql_fetch_array($result)) {

        $gid = $row['gid'];

        echo "GIDs is " . $gid . ".<br />";

}

        $gresult = mysql_query("SELECT * FROM group where gid='$gid'");

        while ($grow = mysql_fetch_array($gresult)) {

        echo $grow['name'];       

        }

}

 

close the first fetch before starting the second

 

Solution2:

if u need the data from the two tables at the same time .... u can make a join relation between 'em if there is any common things and execute a single query with single fetch

 

Solution3:

if u r going to use the first data in order to get the second data ... fetch all the first data and store it inside an array then take 'em from that array .... the make the second fetch

 

I hope this will work, plz try any solution and tell me back about the result

 

Greetings,

Noor

 

 

Link to comment
Share on other sites

As NoorAdiga stated, having looping queries such as that is terribly inefficient. Learn how to do JOINS and you will unlock the real power of a relational database. Also, theres no need to select '*' when you only need a particular field.

 

This single query would get you the same result

 

$query = "SELECT `group.name`
          FROM `group`
          JOIN `groupuser`
            ON `groupuser.gid` = `group.gid`
          WHERE `groupuser.user` = '$username'";

$result = mysql_query($query) or die (mysql_error());

while ($record = mysql_fetch_assoc($result))
{
    echo "Name{$record['name']}<br />";
}

Link to comment
Share on other sites

Hey ...

 

u r making two nested fetch ... I think this is the problem ...

I didn't use mysql b4 , I use Oracle database but there I can't make nested fetch ...

fetch after another

 

MySQL has no problems with that. (And I doubt Oracle actually has, although I never used it)

 

jennatar: You should still think about making it a single query. It will be faster.

Link to comment
Share on other sites

As NoorAdiga stated, having looping queries such as that is terribly inefficient. Learn how to do JOINS and you will unlock the real power of a relational database. Also, theres no need to select '*' when you only need a particular field.

 

This single query would get you the same result

 

$query = "SELECT `group.name`
          FROM `group`
          JOIN `groupuser`
            ON `groupuser.gid` = `group.gid`
          WHERE `groupuser.user` = '$username'";

$result = mysql_query($query) or die (mysql_error());

while ($record = mysql_fetch_assoc($result))
{
    echo "Name{$record['name']}<br />";
}

 

jennatar...definitely look into JOINs (just like noted above). Also, here is the list of reserved words:

http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html

Link to comment
Share on other sites

well ... Oracle has this problem ... I spent a lot of time till I figure out why my code didn't work and after removing the nested fetch it worked well  :D

Mysql is more friendly ... maybe I will convert to it in the next project

 

Regards,

Noor

 

It is not really MySQL vs Oracle we're talking here, but how their relative PHP extensions work.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.