sagar sagar - 3 months ago 7
MySQL Question

How to display and concatenate name of two table in php

$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.

Answer

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

Comments