Question:
PHP & SQL nested queries help?
Cazpa
2009-12-19 11:45:26 UTC
I have a query that I need to loop somehow:

$sql = "SELECT * FROM quotes WHERE user = (SELECT frd_name FROM friends WHERE mem_name = '$profile_user')";



SQL query failed. Check your query.

Error Returned: Subquery returns more than 1 rowSubquery returns more than 1 row


You can see the error that I am getting, I want to be ale to select quotes where user is a friend and this needs to allow for multiple friends.

Please help if you can
Four answers:
2016-05-26 12:00:28 UTC
You only need to connect to the database once per page, assuming both tables you are using are in the same database. The lines $conn= and $db= achieve that for you. The lines $sql_result = mysql_query... and $sql_result2 =mysql_query... have wrong syntax it should be $sql_result = mysql_query($sql) or die(mysql_error()); and $sql_result2 = mysql_query($sql2) or die(mysql_error());
2009-12-19 12:05:32 UTC
One way to do it is:

-> execute the "SELECT frd_name FROM friends WHERE mem_name = '$profile_user'"

-> for each result of the previous query, execute "SELECT * FROM quotes WHERE user = (...)"



Here's how:



$friends = mysql_query("SELECT frd_name FROM friends WHERE mem_name = '$profile_user'");

for($i=0; $i
$friend=mysql_fetch_array($friends);

$friend_name = $friend["frd_name"];

$posts = mysql_query("SELECT * FROM quotes WHERE user = $friend_name");

for($j=0; $j
(...)

}

}
TheMadProfessor
2009-12-21 06:37:01 UTC
"WHERE user =" can only match against a single value. If you want to match against any one of a number of possibilities, you have to specify "WHERE user IN" instead.
lifesaver
2009-12-19 12:51:42 UTC
You may contact an expert programmer/designer live at website like http://www.livefreelancer.net/ etc


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...