0

Look at this fb screenshot 1st upper part has userName and the 2nd half part has pageName so i want same query I have Two Tables userwhich has field userName and another table page which has field pageUserName so im selecting this two tables form the database in while loop but i want to echo this two Names in same while loop one after another like we have in FB their are userName as Well as Page names in news feed how can i do this ????

    <?php  
    $sql=mysql_query("SELECT * FROM user LEFT JOIN page ON user.userID=page.userID") ;
    while($row=mysql_fetch_assoc());
    {
    //For First Loop It Should Be UserName
    echo $row['userName']; 

   //For 2nd Loop It Should Be pageUserName
    echo $row['pageUserName']; 

    } ?>

This is the code but the echo gets print together i want separate echo for userName and another for pageUserName one after another and not together

Abhi Burk
  • 2,003
  • 1
  • 14
  • 21

4 Answers4

0

Try like this:

<?php
$sql = mysql_query("SELECT * FROM user LEFT JOIN page ON user.userID=page.userID");

while ($row = mysql_fetch_assoc()) {
    echo $row['userName'] . " " . $row['pageUserName'] . "<br>";
}
?>

NOTE

You should not use open / end php tag in each row.

You can concatenate your string with . character as in my example.

WARNING

mysql_* functions are deprecated, use mysqli_* or PDO instead.

UPDATE

Based on OP edit in his original question, I think I get it what he wants. When you iterate through your results, instead echo put them into 2 arrays.

Then iterate through both of them. I write for this a little function called showNames, so you avoid code repetation.

function showNames($array) {
    foreach ($array as $item) {
        echo $item."<br>";
    }
}

//Init arrays
$userNames = [];
$pageUserNames = [];

while ($row = mysql_fetch_assoc()) {
    //Put values into the 2 arrays
    $userNames[] = $row['userName'];
    $pageUserNames[] = $row['pageUserName'];
}

//Show the items of arrays
showNames($userNames);
echo '<hr>';
showNames($pageUserNames);
vaso123
  • 12,347
  • 4
  • 34
  • 64
  • Downvoter please explain why did you downvote all the answers? – vaso123 Aug 03 '16 at 09:03
  • We sure did, maybe the english explanation is not the best. But given that there are 4 answers that point to the same solution, it might not be our fault... – Borjante Aug 03 '16 at 10:07
  • i don't want to concatenate those two names but i want those two names separately and not in same echo results one result for username and another result for pageUserName for understanding take example of fb news feed they have different results for user and pages in same query so i want that – Abhi Burk Aug 03 '16 at 12:53
  • Sorry, I am not on facebook. What is the desired result? I dont get it. `one after another` What does it mean? In a new row, or separated by a sapce? Like `Fistname Lastname`? Show me an example here. Anyway, did you tried my code? – vaso123 Aug 03 '16 at 13:15
  • check that image which has separate results for users and pages but in same query i want single query for that kind of result – Abhi Burk Aug 03 '16 at 20:24
  • Thats not errors, I think you mean the array creation. Older PHP can not parse it, change it to `$userNames = array();` – vaso123 Aug 08 '16 at 10:55
0
<?php  
$sql=mysql_query("SELECT * FROM user LEFT JOIN page ON user.userID=page.userID") ;

while($row=mysql_fetch_assoc());
{
    echo $row['userName']." ".$row['pageUserName']."<br />";
} 
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Dennisvdh
  • 120
  • 5
  • i don't want to concatenate those two names but i want those two names separately one after another and not in same echo results one result for username and another result for pageUserName for understanding take example of fb news feed they have different results for user and pages in same query so i want that – Abhi Burk Aug 03 '16 at 12:55
0

inside loop make a single line as follows:

<?php echo $row['userName']. " - ". $row['pageUserName']."<br>" ?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Aravind Pillai
  • 739
  • 7
  • 21
  • i don't want to concatenate those two names but i want those two names separately one after another and not in same echo results one result for username and another result for pageUserName for understanding take example of fb news feed they have different results for user and pages in same query so i want that – Abhi Burk Aug 03 '16 at 12:56
0

You mean this?

<?php  
  $sql=mysql_query("SELECT * FROM user LEFT JOIN page ON user.userID=page.userID") ;
  while($row=mysql_fetch_assoc())
  {
    echo $row['userName'] . ' ' . $row['pageUserName'];
  }
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Borjante
  • 9,767
  • 6
  • 35
  • 61
  • what will be the output for this ? will it be like fb news feed whic displays both users as well as pages in same query ? – Abhi Burk Aug 03 '16 at 09:22
  • Downvoter, if you think the answer is not correct, you could at least point us to what you believe the answer is about. @AbhiBurk This will show "Username - PageUsername" for each row in the query – Borjante Aug 03 '16 at 10:09
  • And as this is basically a _Try This_ answer - Why should the OP "try this"? A **good answer** will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO that may find this question and be reading your answer. – RiggsFolly Aug 03 '16 at 10:22
  • "SELECT * FROM user LEFT JOIN page ON user.userID=page.userID" You say that this query doesn't return the columns from the page table? Are you sure?... – Borjante Aug 03 '16 at 10:22
  • The query DOES indeed get all columns from both tables. The OP asks for "echo this two Names in same while loop one after another". I agree that he probably should not be coding, given the code written, but hey, we tried to help him. But let the OP answer if we solved it or not – Borjante Aug 03 '16 at 10:24
  • Oooppppssssssssss. Edited so I could remove the DV – RiggsFolly Aug 03 '16 at 10:27
  • 1
    In that case this question is so unclear as to what OP actually wants from us I give up – RiggsFolly Aug 03 '16 at 10:30
  • Answer was not finished, requirements were bad and I was trying to get some more info out of it. I know it could be a comment, but common, given his english I thought this would be far easier. Obviusly I would have added the mysql_ problem, and I don't want to spend more time on this, but downvoting all answers just because you think it's not what you would answer ain't right. – Borjante Aug 03 '16 at 10:30
  • Yes, let's leave it here. Sorry about this discussion – Borjante Aug 03 '16 at 10:30
  • Thats not why I did it, I believed the query would not return data from the page table that was why I dv's – RiggsFolly Aug 03 '16 at 10:31
  • Ok, no problem, remove downvotes from other answers and that's it. Cheers – Borjante Aug 03 '16 at 10:31
  • Dont apologise, I was wrong. Put me right anytime I am wrong by all means – RiggsFolly Aug 03 '16 at 10:31
  • For First Loop i want echo to be username and for 2nd pageUserName i dont want to concat this two field at same time with same result – Abhi Burk Aug 03 '16 at 13:08