-4
$query1=mysql_query("select * from table1");
$query2=mysql_query("select * from table2");    
    while($query1_row=mysql_fetch_assoc($query1))
    {

        while($query2_row=mysql_fetch_assoc($query2))
        {
            echo $query1_row['fname'].$query2_row['fname']."<br>";
        }
    }

I have created two table with fname as a field in both the table.I wanted to concatenate every fname in table1 with every other fname in table 2.

sagar
  • 43
  • 2
  • 5
  • What exactly is wrong? What is JavaScript tag for? – k102 Aug 15 '16 at 14:18
  • 3
    know what accepting an answer means btw? don't be a sponge – Funk Forty Niner Aug 15 '16 at 14:20
  • Are you talking to OP? – Ivan Aug 15 '16 at 14:24
  • @Ivan did someone ping you like I just did now? There's your answer ;-) – Funk Forty Niner Aug 15 '16 at 14:26
  • @Fred-ii- I don't get it ;) – Ivan Aug 15 '16 at 14:29
  • @Ivan *"Are you talking to OP?"* - If there is no `@` in comments followed by a member's name, it is usually aimed at the OP. – Funk Forty Niner Aug 15 '16 at 14:30
  • That's not going to work. the inner loop will only execute ONCE, because once a row has been `fetch()`ed, it will not be available to fetch again. Once the second iteration of the other loop starts, there's no more records available for fetching in the $query2 result set. – Marc B Aug 15 '16 at 14:31
  • 1
    @Fred-ii- Oh, ok and you said that because he has a habit of not accepting answers right ? – Ivan Aug 15 '16 at 14:34
  • 2
    @Ivan Exactly. People come here asking many questions, they get their solution, run off without even saying (and at the very least) a "thank you". People here like to help out, but when it comes to a point that they're only being used like a cheap skank, then things start to smell pretty fishy and bad ;-) They want FREE help; ok so they should give a "it won't kill them" FREE mouse tick on the green. It's just ignorance on their part really. They need to know what "good will" is and *"savoir faire"*, if you know what I mean ;-) – Funk Forty Niner Aug 15 '16 at 14:38
  • [***STOP** being this...*](http://www.theblaze.com/wp-content/uploads/2014/01/shutterstock_171162305-620x374.jpg) --- [*Definition:...*](https://en.wikipedia.org/wiki/Sponge) – Funk Forty Niner Aug 15 '16 at 14:46

1 Answers1

0

I cannot think for the life of me why you want to do this but here it is anyway.

If you want to get every row from the second table for every row of the first table then put the second query inside the first while loop. Then you will re-execute the second query once for each of the outer loops rows

$query1=mysql_query("select * from table1");

while($query1_row=mysql_fetch_assoc($query1))
{
    $query2=mysql_query("select * from table2");    
    while($query2_row=mysql_fetch_assoc($query2))
    {
        echo $query1_row['fname'].$query2_row['fname']."<br>";
    }
}

BUT I HAVE TO SAY THIS: Please dont use the mysql_ database extension, it is deprecated (gone for ever in PHP7) Specially if you are just learning PHP, spend your energies learning the PDO database extensions. Start here its really pretty easy

Community
  • 1
  • 1
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149