2

I want to join 2 SQL tables with same attribute but different data and display in 1 PHP Table. Table w2 and w3 and display in 1 PHP table. I am new to PHP so the Code might be very wrong.

    <?php  
    //create database connection 

    require_once('C:\Users\TO115956\Documents\EasyPHP-DevServer-14.1VC11\data\localweb\projects\...\db_w2.php'); 
    require_once('C:\Users\TO115956\Documents\EasyPHP-DevServer-14.1VC11\data\localweb\projects\...\db_w3.php'); 

    //query the db 
    $query = mysqli_query($connect ,"SELECT Avis,Cde_Sap,Don_Ordre,PN_in ,SN_in,DATE2,Statut_Cde FROM w2 , w3") ;
    ?>
    <table>
    <tbody>
    <?php

    while($w2= mysqli_fetch_assoc($query)){
    echo"<tr>";

    echo"<td>".$w2['Avis']."</td>";
    echo"<td>".$w2['Cde_Sap']."</td>";
    echo"<td>".$w2['PN_in']."</td>";
    echo"<td>".$w2['SN_in']."</td>";
    echo"<td>".$w2['DATE2']."</td>";
    echo"<td>".$w2['Statut_Cde']."</td>";
    //  echo"<td>".$w2['Employee']."</td>";
    //  echo"<td>".$w2['Comment']."</td>";

}//end while 
        while($w3= mysqli_fetch_assoc($query)){
    echo"<tr>";

    echo"<td>".$w3['Avis']."</td>";
    echo"<td>".$w3['Cde_Sap']."</td>";
    echo"<td>".$w3['PN_in']."</td>";
    echo"<td>".$w3['SN_in']."</td>";
    echo"<td>".$w3['DATE2']."</td>";
    echo"<td>".$w3['Statut_Cde']."</td>";
//  echo"<td>".$w3['Employee']."</td>";
//  echo"<td>".$w3['Comment']."</td>";
}//end while 
?>
<tbody>
</table>

I want to display both the SQL tables in 1 PHP table.

5
  • The SQL statement will result in a cross join all records related to all records. Is this really what you want or do you want to do a JOIN so that only related records are matched together? Regardless when the query executes this will generate one result set. You shouldn't have to iterate though each table separately. Query to database returns result set . You process the result set in Php To display the data. Since a result set combines the tables for you already, all you should have to do is reference the result set and columns desired to be displayed. Commented Apr 27, 2015 at 13:47
  • Thanks for your help @xQbert . It is almost similar to situation of displaying all the data of my both the tables but the data does not have any connection . They only have the same table attributes . Suggest what to do now . Thank you in advance . Commented Apr 27, 2015 at 13:54
  • If you can provide more information about the two tables then we can see how it can be done Commented Apr 27, 2015 at 14:01
  • instead of a JOIN cross /join use a UNION statement. SELECT Avis,Cde_Sap,Don_Ordre,PN_in ,SN_in,DATE2,Statut_Cde FROM w2 UNION ALL SELECT Avis,Cde_Sap,Don_Ordre,PN_in ,SN_in,DATE2,Statut_Cde FROM w3 UNION if you want to eliminate duplicates. Union All if you want to keep duplicates. Commented Apr 27, 2015 at 14:01
  • Thankyou @xQbert UNION ALL works perfectly for me . Commented Apr 27, 2015 at 14:08

2 Answers 2

1

Use a union. I don't think you want a cross join here given desired results and comments...

SELECT Avis,Cde_Sap,Don_Ordre,PN_in ,SN_in,DATE2,Statut_Cde 
FROM w2 
UNION ALL 
SELECT Avis,Cde_Sap,Don_Ordre,PN_in ,SN_in,DATE2,Statut_Cde 
FROM w3
Sign up to request clarification or add additional context in comments.

Comments

0

I would recomend you to use an inner join (or left or right join) and if you are concerned about performance try using temporary tables.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.