Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL query WHERE statement which is a query itself?
    text
    copied!<p>So I'll try to clearly explain my goal first:</p> <p>First, I want to query one table in my database for a list of usernames.</p> <p>Second, I want to take those usernames, and query another table in the database and return only the rows in which these usernames appear in the username field.</p> <p>Finally, I want to take this output (in JSON array form right now), and return it to the requesting client.</p> <p>My query looks like this right now:</p> <pre><code>$query = mysql_query("SELECT * FROM tagusers WHERE username = (SELECT userA FROM friendtable WHERE userB = '$username')"); </code></pre> <p>This works when the WHERE statement yields 1 result. So if I only get one returned userA, it works fine. But if I get multiple, I get a bunch of errors.</p> <p>Here is the code in its entirety:</p> <pre><code>if (isset($_POST['username'])) { $username = $_POST['username']; $connect = mysql_connect("localhost", "root", ""); mysql_select_db("TagDB"); $query = mysql_query("SELECT * FROM tagusers WHERE username = (SELECT userA FROM friendtable WHERE userB = '$username')"); } while ($e = mysql_fetch_assoc($query)) { $output[] = $e; } $output = json_encode($output); print $output; </code></pre> <p>I get the following error on the query line:</p> <p>*Warning: mysql_query() [function.mysql-query]: Unable to save result set in C:\wamp\www\tag\appgetfriendinfo.php on line 21*</p> <p>So all I really need to know is, how would I write that query in MySQL so that I get returned an array of rows?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload