How to get 2 people posts from this code?

function all($id){
$query = mysql_query(“SELECT * FROM following WHERE id_user = $id”);
$rows_q = mysql_fetch_array($query);

	$n_query = $rows_q['id_sub'];
	$querys = mysql_query("SELECT * FROM ar WHERE id_menu = $n_query");

  while($row = mysql_fetch_array($querys)){
	$out .= '
        <h4>'.$row['title'].' ?</h4>
return $out;


Please place code inside of the PHP tags. It helps us a lot!

Now, what are you asking? Do you want to select two users from your queries? You showed some code, but,
you didn’t explain what you need help with. “2 people posts”???

If you mean to id’s that are in table “following” ??? You would just change your query to select both of the two
id’s… Loosely like:
$query = mysql_query(“SELECT * FROM following WHERE id_user = $id OR id_user = $id2”);
Of course, this would create a list of two items so to get your “id_sub” you would have to loop thru the two
of them…

Well, not sure if this helps. Perhaps you can rephrase your needs…

br1ck, you should not really be using MySQL, as it is deprecated and no longer used. You should upgrade to the
improved version called MySQLi. Most servers will stop allowing MySQL soon. Most are actually pushing for all
code to use PDO. But. this is not important for your problem…

Now, you run a query. You select all of the id_sub’s from that user. Then, you take JUST THE FIRST ONE from
the list and run a query against that one and only one item. I think you now understand the issue. The second
query must either be handled in a loop as you go thru ALL of the id_sub’s that were found or the query must be
altered to include all of id_sub’s found in the first query.

This can be done in one query, but, is tricky. It is easier to just do two loops. The first loop parse thru all of the
id_sub’s found and the second thru all of the titles found… Loosely something like this:
function all($id){
$query = mysql_query(“SELECT * FROM following WHERE id_user = $id”);
while ($row = mysql_fetch_array($query)) {
$n_query = $row[‘id_sub’];

     $query2 = mysql_query("SELECT * FROM ar WHERE id_menu = $n_query");
     while ($row2 = mysql_fetch_array($query2)) {
        $out .= '<h4>' . $row2['title'] . ' ?</h4>';
  return $out;

So, basically, there is two loops. One looping thru the id_sub’s and the second looping thru the titles…
I renamed the queries 1 and 2 and the row’s 1 and 2 so you can follow it…

Now, in MySQL (any version), you can query two at a time and tell it to select values based on ones found
in the other query. If you did that, you would only need one loop. But, this is sometimes hard to understand
if you are not experienced in SQL queries. At this point I would just use the double loop…

Hope that makes sense and helps! Glad to see you back at the site…

[member=43746]ErnieAlex[/member] I don’t agree we should skip hoops just because a concept is harder to understand. Spending a little time figuring out joins is cruical to be able to work with databases, the query is not really that hard, and you end up with much simpler code. Also I’m confused as to why you suggest mysqli when you say yourself the community is pushing to make PDO standard.


SELECT title FROM ar JOIN following ON ar.id_menu = following.id_sub WHERE following.id_user = $id

So your code should be (untested):

[php]function all($id){
$sql = “SELECT title
JOIN following ON ar.id_menu = following.id_sub
WHERE following.id_user = $id”;

$query = mysql_query($sql);
while($row = mysql_fetch_array($querys)) {
$out .= ‘

’.$row[‘title’].’ ?


return $out;

Selecting the title field only, as that’s the only thing you’re using. Note that you can use SELECT * (though manually selecting the fields you want is best) to get all fields (from both tables).

The JOIN combines the tables. You’re written yourself that you want the rows from the table “ar” where “id_menu” equals the rows from “id_sub” in the “following” table. So we’re basically just saying the same thing in this JOIN.

And finally we add the same condition as you initially had, WHERE id_user = $id


That said, use PDO (or at least mysqli), both of these help you secure your code as it’s very easy to slip up and leave your code vulnerable to sql injection hacks using the older mysql_* functions. Not to mention mysql_* is removed from PHP7.

That site is a recommended read. You will also spot other areas of improvement, like not mixing in html like you do in this function.

Sponsor our Newsletter | Privacy Policy | Terms of Service