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?
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
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))
Separate the 2 queries and their results
merge both results into an array
sort the array as needed
output the array (generate table rows)
Related
I'm currently trying to print the result of joining 2 tables using mysqli. but everytime i try to do it, it only return the result from one table while the table that it's joining doesn't return any result
below is the code that show the data into a table
foreach($db->showdata() as $x) {
if($x['role_id'] == 1 || $x['role_id'] == 2) continue;
$id = $x['user_id'];
echo "<tr>";
echo "<td>" . $x['user_id'] . "</td>";
echo "<td>" . $x['first_name'] ." ". $x['last_name']. "</td>";
echo "<td>" . $x['nilai_tugas'] . "</td>";
echo "<td>" . $x['nilai_uts'] . "</td>";
echo "<td>" . $x['nilai_uas'] . "</td>";
echo "</tr>";
}
while this one is the showdata() function it called
function showdata(){
$data = mysqli_query($this->db, "SELECT * FROM user LEFT OUTER JOIN grade ON(user.user_id = grade.user_id) IS NOT NULL;");
while($d = mysqli_fetch_array($data)){
$hasil[] = $d;
}
return $hasil;
}
The structure of the table is
User: user_id, first_name, last_name and other column that's irrelevant for this part
Grade: user_id, nilai_tugas, nilai_uts, nilai_uas
when I run the code it return
Notice: Undefined index: nilai_tugas
Notice: Undefined index: nilai_uts
Notice: Undefined index: nilai_uas
which is from the grade table, but the data from the user table do go through and show up in the page.
The page result
When I run the query in mysql it does return all the data I requested but it doesn't seem to return the data when in the function.
Thank you in advance.
Edit: I tried to move the select query into the same page and it work, it just don't work when I use return from function(which is mandatory).
Can you try be more explicit in selecting your columns?
mysqli_query($this->db, "SELECT user.columnname, grade.columnname FROM user LEFT OUTER JOIN grade ON(user.user_id = grade.user_id) IS NOT NULL;");
And are you sure the column in users is called user_id? Because the result set has the columnname studentID.
I don't fully know what happened, so I tried reinstalling xampp and it finally start working, the problem might be located in mysql config file(not the first time this happen to me).
im trying to combine two tables together, but whenever i run the program, this happens.
As you can see i've echoed out the sql statement. Here are my codes.
$queryc1 = "select sum(repeater),sum(membersigned) from sales UNION ALL select count(*) from approach;"; //DO INNERJOIN PRACTISE
$resultc1 = mysqli_query($dbconn,$queryc1);
echo "<table>
<tr>
<th>Repeater</th>
<th>Members</th>
<th>Approach</th>
</tr>";
while($row = mysqli_fetch_array($resultc1)) {
echo "<tr>";
echo "<td>" . $row['sum(repeater)'] . "</td>";
echo "<td>" . $row['sum(membersigned)'] . "</td>";
echo "<td>" . $row['count(*)'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo $queryc1;
I'd like to show count(*) as in the photo, as a third column to the genrated table.
You don't need UNION select here, instead you can use subquery:
select sum(repeater) as repeater_sum,
sum(membersigned) as membersigned_sum,
(select count(*) from approach) as approach_count
from sales;
and in PHP you use $row['repeater_sum'].. etc
When you union two or more queries together each query should have the same columns of data with same data type for example :
SELECT SUM(repeater),SUM(membersigned) FROM sales
UNION
SELECT Text1,Text2 FROM approach
Replace your query with this format
I had an assessment on pulling data from a table but i had to use two tables to do a calculation, i figured out a way to do it but it doesn't look good neither it would be good to keep using this way
$query = mysqli_query($connection, "SELECT * FROM metal");
$query2 = mysqli_query($connection, "SELECT * FROM conversion WHERE type = 'EUR'");
$data2 = mysqli_fetch_array($query2);
$query3 = mysqli_query($connection, "SELECT * FROM conversion WHERE type = 'USD'");
$data3 = mysqli_fetch_array($query3);
while($data = mysqli_fetch_array($query)){
echo "<tr>" . "<td>" . $data['metal'] . "</td>" . "<td>" . $data['cost'] . "</td>" . "<td>" . ($data['cost'] * $data3['amount']) . "</td>" . "</tr>";
}
This grabs cost on metal type then using another table calculates cost in Euro and USD, I don't want to use 3 querys and fetch them 3 times as it isn't computationaly effective is there a better way to do it
You'd need at least two, since you're fetching from two essentially unrelated tables
SELECT * FROM metal
SELECT * FROM conversion WHERE type IN ('EUR', 'USD')
the second query will fetch BOTH records, and then you'll just need some extra code to sort out which record is which.
Could you please post your overall table structure? In case you have a relation between these two table you could just join it.
I have a table with two collumns (shortened), NAME and CATEGORY.
I want to output the number of distinct categorys. For an examle: Sport : 5 , Houses : 10.
I use this one:
$test = mysqli_query($con,"SELECT category, COUNT(category) as count FROM tablename GROUP BY category ORDER BY count DESC");
This work then I run the code in SQL Shell, but I have no clue on how to output it in PHP. I have searced Google up and down without any successfull solution.
Any help?
I want to output it in a table format.
EDIT: Here is my full code: (tablename is changed, and $con is removed)
$test = mysqli_query($con,"SELECT DISTINCT lkategori, COUNT(lkategori) as count FROM tablename GROUP BY lkategori ORDER BY count DESC");
while($row = mysql_fetch_array($test)) {
echo $row['lkategori'] . ":" . $row['count'];
die("test");
}
$test = mysqli_query($con,"SELECT DISTINCT lkategori, COUNT(lkategori) as count FROM tablename GROUP BY lkategori ORDER BY count DESC");
echo "<table border='1'>";
while($row = mysqli_fetch_array($test)) {
echo "<tr>";
echo "<td>" . $row['lkategori'] . "</td>";
echo "<td>" . $row['count'] . "</td>";
echo "</tr>";
}
echo "</table>";
This will output all the categories and the count returned by the sql statement into a table. Also as a sidenote you should look into PDO.
EDIT: to make sure you do get the distinct values you should use the DISTINCT keyword in your sql statement:
$test = mysqli_query($con,"SELECT DISTINCT category, COUNT(category) as count FROM tablename GROUP BY category ORDER BY count DESC");
use this
while($row = mysqli_fetch_array($test)) {
echo $row['lkategori'] . ":" . $row['count'];
die("test");
}
Thanks
Have 3 different mysql tables, my script queries the DB using JOINS to output the most recent results from each of the 3 tables. All works like it is supposed to but the way mysql databases allow for JOINS, the ORDER BY will order the results as you list the tables, resulting in the finished output not being ordered by date correctly, since the results are from 3 different MYSQL tables.
The results instead are ordered by date correctly only next to the other results form the same table. See the script and results below: (I am limit 1 for the output for simplicity)
$username = $_SESSION['username'];
$result = $mysqli->query("
SELECT
accounts.account_name,
cases.case_subject,
tasks.task_title,
accounts.accounts_date_last_edited,
cases.cases_date_last_edited,
tasks.tasks_date_last_edited
FROM
accounts,
cases,
tasks
WHERE
accounts.username = cases.username
AND cases.username = tasks.username
ORDER BY
accounts.accounts_date_last_edited DESC,
cases.cases_date_last_edited DESC,
tasks.tasks_date_last_edited DESC
LIMIT 1
");
if (!$result) { echo "Error Result5: " . $mysqli->error; } else {
echo "<table>";
echo "<tr><th>Item</th><th>Date</th></tr>";
while ($row = $result->fetch_array(MYSQLI_ASSOC))
{
echo "<tr>";
echo "<td>" . $row['account_name'] . "</td><br>";
echo "<td>" . $row['accounts_date_last_edited'] . "</td><br>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['case_subject'] . "</td><br>";
echo "<td>" . $row['cases_date_last_edited'] . "</td><br>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['task_title'] . "</td><br>";
echo "<td>" . $row['tasks_date_last_edited'] . "</td><br>";
echo "</tr>";
}
echo "</table>";
The Results are below: they will list 1 item from each table listed above. Again I am trying to sort by the date_last_edited regardless of which table the values are from.
Item Date
Shorley Homes 2013-11-02 04:02:34
Techical Testing Case Open 2013-11-03 07:17:36
Icons 2013-11-03 07:28:02
If I read correctly (and I'm not sure of that) your
...sort by the date_last_edited regardless of which table the values are from...
then you might want to use UNION instead of JOIN
SELECT account_name item, accounts_date_last_edited date
FROM accounts
UNION ALL
SELECT case_subject, cases_date_last_edited
FROM cases
UNION ALL
SELECT task_title, tasks_date_last_edited
FROM tasks
ORDER BY date DESC
Here is SQLFiddle demo