Continue MYSQL output in PHP - php

I have a select on a database, like this:
$result = mysql_query("
SELECT dat_eb_registrants.id, dat_eb_registrants.first_name,
dat_eb_registrants.last_name, dat_eb_registrants.email, dat_eb_registrants.comment,
dat_eb_registrants.amount, dat_eb_registrants.published,
dat_eb_registrants.transaction_id, dat_eb_registrants.register_date,
dat_eb_field_values.field_value
FROM dat_eb_registrants LEFT JOIN dat_eb_field_values
ON dat_eb_registrants.id=dat_eb_field_values.registrant_id
WHERE dat_eb_field_values.field_id='53' AND `event_id` >= 20 AND `event_id` <= 25
ORDER BY $sort $ascdsc
");
and it gets diplayed in a html table like so:
echo "
ID
First name
Last name
Email
Comment
Value1
Value2
Value3
";
while ($row = mysql_fetch_row($result)) {
echo "<tr>";
echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "<td>" . $row[3] . "</td>";
echo "<td>" . $row[4] . "</td>";
echo "<td>" . $row[9] . "</td>";
echo "<td>" . $row[?] . "</td>";
echo "<td>" . $row[?] . "</td>";
echo "</tr>";
echo "</table>";
}
Now, the first 4 values are being displayed correctly, just like the 5th one, but how about the 6th and the 7th one? they are not being called by MYSQL because dat_eb_field_values.field_value is only called one time, and is assigned the value WHERE dat_eb_field_values.field_id='53
How can i complete the table with the other values in the database?
Thanks in advance, Laurent

If I understand correctly, you are trying to load multiple values for each person. These values are in multiple rows in a separate table.
You'll need to use a GROUP BY clause. JOIN the registrants table with the values table just like you do now, and after the WHERE clause, GROUP BY the registrant's id. And of course, don't restrict the WHERE to a certain value's ID, because then you won't get any other values. Finally, you can use GROUP_CONCAT() to get all the value rows into a single result value, delimited by ,. You can change this to be delimited by </td><td> to make it easier to put into your table.
SELECT registrants.name, GROUP_CONCAT(values.value SEPARATOR '</td><td>')
FROM registrants
LEFT JOIN values USING (registrant_id)
WHERE ...
GROUP BY registrant_id

Voitek Zylinski answer is I think the best way to do this. but you could try a for each.
while ($row = mysql_fetch_row($result))
{
echo "<tr>";
foreach($row as $row_item)
{
echo "<td>".$row_item."</td>"
}
echo "</tr>";
}
This would work no matter how many field

Related

Trying to join different table from different database in the localhost, Used MYSQL

Explanations:
First of all im new in the database field, i got system from my boss, my boss ask me to create dashboard based on the data from database, but some data need to join other table in different database. In my case i need to fetch data at column score from table rank in 4 different database which is "virtualexam, virtualexam1, virtualexam2, virtualexam3,"
. I had try and search but i cant display the data into the table, your advice and recommendation really appreciate it.. Thank you for you kindness
Error picture
click here
Database Table Picture *Dummy data
db.virtualexam
db.virtualexam1
db.virtualexam2
db,virtualexam3
my query
<?php
$sql = "SELECT virtualexam.rank.id, virtualexam.rank.username,
virtualexam.rank.score AS score, virtualexam1.rank.score AS 'score1', virtualexam2.rank.score AS 'score2', virtualexam3.rank.score AS 'score3'
SUM(virtualexam.rank.score + virtualexam1.rank.score + virtualexam2.rank.score + virtualexam3.rank.score ) AS 'total'
FROM virtualexam.rank
JOIN virtualexam1.rank ON virtualexam.rank.username = virtualexam1.rank.username,
JOIN virtualexam2.rank ON virtualexam1.rank.username = virtualexam2.rank.username,
JOIN virtualexam3.rank ON virtualexam2.rank.username = virtualexam3.rank.username
GROUP BY id ";
$result = $conn->query($sql);
?>
display in the table
<tbody>
<?php
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['score'] . "</td>";
echo "<td>" . $row['score1'] . "</td>";
echo "<td>" . $row['score2'] . "</td>";
echo "<td>" . $row['score3'] . "</td>";
echo "<td>" . $row['total'] . "</td>";
echo "</tr>";
}
?>
</tbody>
$sql = "SELECT virtualexam.rank.id, virtualexam.rank.username,
virtualexam.rank.score AS score, virtualexam1.rank.score AS 'score1', virtualexam2.rank.score AS 'score2', virtualexam3.rank.score AS 'score3',
SUM(virtualexam.rank.score + virtualexam1.rank.score + virtualexam2.rank.score + virtualexam3.rank.score ) AS 'total'
FROM virtualexam.rank
JOIN virtualexam1.rank ON (virtualexam.rank.username = virtualexam1.rank.username)
JOIN virtualexam2.rank ON (virtualexam2.rank.username = virtualexam1.rank.username)
JOIN virtualexam3.rank ON (virtualexam3.rank.username = virtualexam2.rank.username)
GROUP BY virtualexam.rank.username";

Adding a rank to my mySQL output data [duplicate]

This question already has answers here:
ROW_NUMBER() in MySQL
(26 answers)
Closed 6 years ago.
The code below is returning a table with data from mysql, but what I'm missing in the output is a generated column with a rank based on a column's data (wilks in this example). I've looked for examples and answers to similar questions, but I can't get it to work.
In short: I need an extra column next to the ID column where a rank beginning from 1 is displayed, based on the data from the Wilks column.
Thank you so much!
<?php
$con=mysqli_connect("localhost", "user", "password", "dbname");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM `Mannen` ORDER BY `Wilks` DESC");
echo "<table border='1'>
<tr>
<th>#</th>
<th>Naam</th>
<th>Lichaamsgewicht</th>
<th>Vereniging</th>
<th>Squat</th>
<th>Bench</th>
<th>Deadlift</th>
<th>Totaal</th>
<th>Wilks</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Naam'] . "</td>";
echo "<td>" . $row['Lichaamsgewicht'] . "</td>";
echo "<td>" . $row['Vereniging'] . "</td>";
echo "<td>" . $row['Squat'] . "</td>";
echo "<td>" . $row['Bench'] . "</td>";
echo "<td>" . $row['Deadlift'] . "</td>";
echo "<td>" . $row['Totaal'] . "</td>";
echo "<td>" . $row['Wilks'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
First, if you just want an enumerated value, you can get that using PHP.
But, it is easy enough in MySQL, using variables:
SELECT m.*, (#rn := #rn + 1) as rank
FROM Mannen m CROSS JOIN
(SELECT #rn := 0) params
ORDER BY Lichaamsgewicht DESC;
Technically, this implements the related function of row_number() rather than rank(). Your question is unclear on what you mean by "rank".

Very strange SQL error where data is not being pulled correctly

I have two pages that are very similar, but do slightly different things. However, both place data into a table called playerRegSeason.
Here is the SQL code I use on one page:
$sql2="INSERT INTO playerRegSeason
(playerID, year, teamID, gp, minutes, pts, oreb, dreb, reb,
asts, stl, blk, turnover, pf, fga, fgm, fta, ftm, tpa, tpm)
SELECT playerID, $year, '$teamID', $gp, $minutes, $pts, $oreb,
$dreb, $reb, $asts, $stl, $blk, $turnover, $pf, $fga, $fgm,
$fta, $ftm, $tpa, $tpm FROM players WHERE firstname='".$firstname."'
AND lastname='".$lastname."' AND firstseason='".$firstseason."'";
And here's the code for the other page:
$sql2="INSERT INTO playerRegSeason
(playerID, year, teamID, gp, minutes, pts, oreb, dreb, reb,
asts, stl, blk, turnover, pf, fga, fgm, fta, ftm, tpa, tpm)
VALUES ($playerID, $year, '$teamID', $gp, $minutes, $pts, $oreb,
$dreb, $reb, $asts, $stl, $blk, $turnover, $pf, $fga, $fgm,
$fta, $ftm, $tpa, $tpm)";
On both pages, when I look at the database, the data has been added and is in the proper format.
Now, is my problem. I have another page that displays the contents of the table. Here is the code that is not working:
$quer1 = "SELECT * FROM players p
INNER JOIN playerRegSeason pr ON p.playerID = pr.playerID
INNER JOIN teams t ON pr.teamID = t.teamID
WHERE p.firstname='$firstname' AND p.lastname='$lastname'";
$result1 = mysqli_query($con, $quer1);
while($row = mysqli_fetch_array($result1)) {
echo "<tr>";
echo "<td>" . $row['year'] . "</td>";
echo "<td>" . $row['location']." ". $row['name'] . "</td>";
echo "<td>" . $row['leag'] . "</td>";
echo "<td>" . $row['gp'] . "</td>";
echo "<td>" . $row['minutes'] . "</td>";
echo "<td>" . $row['pts'] . "</td>";
echo "<td>" . $row['oreb'] . "</td>";
echo "<td>" . $row['dreb'] . "</td>";
echo "<td>" . $row['reb'] . "</td>";
echo "<td>" . $row['asts'] . "</td>";
echo "<td>" . $row['stl'] . "</td>";
echo "<td>" . $row['blk'] . "</td>";
echo "<td>" . $row['turnover'] . "</td>";
echo "<td>" . $row['pf'] . "</td>";
echo "<td>" . $row['fga'] . "</td>";
echo "<td>" . $row['fgm'] . "</td>";
echo "<td>" . $row['fta'] . "</td>";
echo "<td>" . $row['ftm'] . "</td>";
echo "</tr>";
echo "</table>";
}
The weird thing is that any data entered from the first sql will not show up, but any data that was entered through the second sql statement will. Even though both entries are listed right next to each other in the db with the same playerID primary key.
You are doing two inner joins, you already confirmed one of them when you said that both entries have the same player ID, but do they also both have the same teamID?
If the first entry doesn't have a teamID that exists in the teams table, it won't be coming in through your query.
A quick way to check this is to change the 'INNER JOIN' on the teams table with an LEFT JOIN, if this gets you more records then your data has some teamID values that don't exist in the teams table.
In the second set of sql inserts you're always going to do an insert (assuming no errors with variables, etc.)
In the first set of sql, you're doing a select statement to perform the insert, specifically look at your where clause - if that clause eliminates all returned results, then no records will be inserted. If you run the statement:
SELECT *
-- input your expected values in place of the variables
FROM players WHERE firstname= ''
AND lastname='' AND firstseason='';
you'll likely see that nothing is being returned from this query, therefore nothing would be inserted in the first query.
Based on your comment it sounds like the issue (since it wasn't due to the first sql statement) would likely be due to the inner joins:
try your query as such:
SELECT * FROM players p
--INNER JOIN playerRegSeason pr ON p.playerID = pr.playerID
--INNER JOIN teams t ON pr.teamID = t.teamID
WHERE p.firstname='$firstname' AND p.lastname='$lastname
See if you're getting your expected value. If you are, uncomment the inner join on playerRegSeason, if still getting your expected row, uncomment the join on teams. The likely cause is that at least one of the tables you're joining on does not contain the record you're expecting, and therefore is not being returned by the query. (or as David suggested, change your inner joins to left joins for testing purposes)

Trying to order a 3 table JOIN by date desc, How to sort correctly?

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

how do you join two tables

i have a mysql query which is trying to join to tables to show the data from two tables onto one table on the webpage?
<?php
include 'library/connect.php';
$result = mysql_query("SELECT * FROM meetings INNER JOIN rooms USING ('room', 'date', 'time' ) ");
echo "<table border='1'><tr><th>Title</th><th>Chairman</th><th>Secretary</th><th>Terms of Reference</th><th>Named membership</th><th>Occurences</th><th>Room</th><th>Date</th><th>Time</th></tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['title']. "</td>";
echo "<td>" . $row['chairman']. "</td>";
echo "<td>" . $row['secretary']. "</td>";
echo "<td>" . $row['termsOfReference']. "</td>";
echo "<td>" . $row['occurences']. "</td>";
echo "<td>" . $row['room']. "</td>";
echo "<td>" . $row['date']. "</td>";
echo "<td>" . $row['time']. "</td>";
echo "</tr>";
}
echo "</table>";
include 'library/closedb.php';
?>
do i need both table id somewhere?
I'd doubt that date and time are columns in your rooms table, so they shouldn't be part of the JOIN condition. Try:
SELECT *
FROM meetings m
INNER JOIN rooms r
ON m.room = r.room
By "Using" keyword it will check both tables by column names. It will check inner join on Ist condition then it will get left join on next columns if result not found.
it's bad practice to use "Using" keyword.
Simplify joins by USING.
By
-Multiple columns
-Single column

Categories