2

I have 2 tables called 0_vrs_american and 0_vrs_europe, and I need to display the rows of these tables in a single html table. So I wrote this code:

<?php
$con=mysqli_connect("localhost","aaa","bbb","my_mk7vrlist");

$result = mysqli_query($con,"SELECT 0_vrs_american.*, 0_vrs_europe.* FROM 0_vrs_american, 0_vrs_europe ORDER BY `vrs` DESC LIMIT 0 , 200");

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $x . "</td>";
  echo "<td>" . $row['playername'] . "</td>";
  echo "<td>" . $row['contactable'] . "</td>";
  echo "<td>" . $row['vrs'] . "</td>";     
  echo "</tr>";
  $x = $x+1;
  }

mysqli_close($con);
?> 

I am pretty new with MySQL and so I googled about this topic and I found a syntax like the one you can see above. By the way I have no results in my page because the table is empty.

So: I must display in a HTML table the content of both sql tables, and the rows must be sorted according with the vrs (number that goes from 50000 to 99000). How could I solve my problem?

4
  • what exactly is wrong with the code above Commented Mar 1, 2014 at 0:10
  • You try to match the rows of the two tables in the code. Is it really what you want? Don't you rather need to display 8 rows if the tables have 5 and 3 rows respectively? BTW: mysqli functions are outdated, use PDO; and always output document strings through htmlspecialchars() unless you are completely positive that it may not be misinterpreted as HTML code! Commented Mar 1, 2014 at 0:10
  • 1
    @Lewyx Mysqli? outdated? What are you going on about Commented Mar 1, 2014 at 0:10
  • @Ohgodwhy: stackoverflow.com/questions/13569/… - I stand corrected that "outdated" may not be the right term, but a beginner is better off learning and using PDO and then reaching for mysqli for particular reasons (if need be) than the other way around. Commented Mar 1, 2014 at 0:21

3 Answers 3

2

An alternative to the above is to select all the rows from those two tables as an inner select, and then order by vrs in the returned result set.

SELECT  *
    FROM
    (
       SELECT * FROM 0_vrs_american
       UNION
       SELECT * FROM 0_vrs_europe
    ) AS a
ORDER BY vrs
Sign up to request clarification or add additional context in comments.

Comments

0

at first U need to define $x before the loop then use the query like this

$result = mysqli_query($con,"SELECT 0_vrs_american.*, 0_vrs_europe.* FROM 0_vrs_american, 0_vrs_europe ORDER BY `vrs` DESC LIMIT 0 , 200") or die (__LINE__." ".mysqli_error($con));

so you will see the error and the line of the query

i just add or die (__LINE__." ".mysqli_error($con))

Comments

-1
  1. Separate the 2 queries and their results
  2. merge both results into an array
  3. sort the array as needed
  4. output the array (generate table rows)

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.