Jump to content

[SOLVED] Sub query


benji87

Recommended Posts

Hi guys i can work out why im getting unexpected T_STRING in this query, can someone help?

 

<?
$sql =("SELECT * FROM pl_users WHERE username = (SELECT username FROM pl_grp WHERE value = '2')") or die mysql_error());
$result = mysql_query($sql);
?>

 

cheers

Link to comment
Share on other sites

Hi

 

You have a bracket at the beginning of the $sql variable, and the "or die shoub be on the next line

 

<?php
$sql ="SELECT * FROM pl_users WHERE username = (SELECT username FROM pl_grp WHERE value = '2')";
$result = mysql_query($sql) or die(mysql_error());
?>

 

Cheers,

tdw

Link to comment
Share on other sites

Um, i dont work much with mysql queries inside mysql queries, but first of all, you are trying to perform a sub search without defining the sub search as a mysql query itsself, plus with all the () you added in addition that could make things harder (with getting the syntax right)

 

here's something i typed out for you

 

<?
$result = mysql_query("SELECT * FROM pl_users WHERE username = \"" . mysql_query("SELECT username FROM pl_grp WHERE value = '2'") . "\");
?>

 

Edit, and yeah the guy above is also correct:

 

<?
$result = mysql_query("SELECT * FROM pl_users WHERE username = \"" . mysql_query("SELECT username FROM pl_grp WHERE value = '2'") . "\") or die(mysql_error());
?>

Link to comment
Share on other sites

Cheers for the help guys darkwinter and sw0o0sh that query worked great but only returned one result. As when there was more than one value that = 2 it came up with an error saying the result could not be saved because the query returned more than one result.

 

So i guess this is something to do with the PK like Patrick said. So i changed it to the query he gave me but i still only seem to get one result when i should get two.

 

Here is my query as it stands:

 

<?php
$sql ="SELECT * FROM pl_users WHERE username IN (SELECT username FROM pl_gpr WHERE value = '2')";
$result = mysql_query($sql) or die(mysql_error());
?>

 

Cheers

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.