Jump to content

Recommended Posts

Can the following code be shortened to use just one query?

 

At the moment it grabs a list of contacts from the db then checks the database for the users details, i know there is a way to combine the 2 querys into one but i cant remember how its done, any help would be much appreciated.

 

Thanks

 

Tommy

 

	$getcontacts = mysql_query("SELECT contactID FROM usergroups WHERE groupID = '$_GET[groupID]'")or die(mysql_error());
if(!mysql_num_rows($getcontacts)> 0)
echo"No contacts in this group!";
else
while($contact = mysql_fetch_array($getcontacts))
{
	$getcontact = mysql_query("SELECT * FROM users WHERE userID = '$contact[contactID]'")or die(mysql_error());
	$user = mysql_fetch_assoc($getcontact);

echo"$user[fullName] - $user[username] - $user[email] - $contact[contactID]<br/>";
};

Link to comment
https://forums.phpfreaks.com/topic/223008-can-this-double-query-be-shortened/
Share on other sites

All it means is that MYSQL doesn't know from which table `userID` you're referring to. You can fix it by referencing the table name before the column name, or by using an alias of the table name to keep it cleaner:

 

SELECT u.* FROM usergroups ug
JOIN users u ON ug.contactID = u.userID
WHERE ug.groupID = 123

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.