I am trying to display data from multiple rows in sql in a single table box.
in the format of:
"MovieTitle" "movieReleaseYear" "directorName"
The Matrix 1999 Andy Wachowski, Lana Wachowski
where Andy Wachowski and Lana Wachowski comes from different rows but are gathered with group_concat
I have no problem getting them in phpmyadmin but i don't know how to display it in php.
I have this:
$sql="SELECT 725G54_5_movies.MovieTitle, 725G54_5_movies.movieProductionYear, GROUP_CONCAT( 725G54_5_director.directorName )
FROM 725G54_5_movies
JOIN 725G54_5_directed ON 725G54_5_movies.MovieID = 725G54_5_directed.movieID
JOIN 725G54_5_director ON 725G54_5_directed.directorID = 725G54_5_director.directorID
GROUP BY 725G54_5_movies.MovieTitle
ORDER BY $order ASC";
$result=mysql_query($sql);
//Presentation av kontakterna via while-sats till ett formulär
while($rows=mysql_fetch_array($result)){
echo "<tr>
<td>"; echo $rows['MovieTitle']; echo "</td>
<td>"; echo $rows['movieProductionYear']; echo "</td>
<td>";
while($director=mysql_fetch_array($rows['directorName'])){ echo $director; };
echo"</td>
</tr>";
Group Concat has already concatenate the name of the directors, so you don't need to iterate on $rows['directorName'] .This part in your code id wrong because $rows is a row from the query result and $rows['director'] is a value from this row, you can't iterate on a value.
$sql = "SELECT GROUP_CONCAT( 725G54_5_director.directorName ) AS directorNames ..."
while($rows=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $rows['MovieTitle'] . "</td>";
echo "<td>" . $rows['movieProductionYear'] . "</td>"
echo "<td>" . $rows['directorNames'] . "</td>"
echo"</tr>";
}
You use GROUP BY 725G54_5_movies.MovieTitle so directorNames field will be the result of all directorName concatenated for this Movie.
The crucial bit of information is that you need to alias your GROUP_CONCAT so that you can access this in the array. I would also use a Heredoc when outputting blocks of html for readability:
$sql=
"SELECT
725G54_5_movies.MovieTitle,
725G54_5_movies.movieProductionYear,
GROUP_CONCAT(725G54_5_director.directorName) directorNames
FROM
725G54_5_movies
JOIN
725G54_5_directed
ON 725G54_5_movies.MovieID = 725G54_5_directed.movieID
JOIN
725G54_5_director
ON 725G54_5_directed.directorID = 725G54_5_director.directorID
GROUP BY
725G54_5_movies.MovieTitle
ORDER BY
$order ASC";
$result=mysql_query($sql);
//Presentation av kontakterna via while-sats till ett formulär
echo "<table>";
while($rows=mysql_fetch_array($result)){
echo <<<HTML
<tr>
<td>{$rows['MovieTitle']}</td>
<td>{$rows['movieProductionYear']}</td>
<td>{$rows['directorNames']}</td>
</tr>
HTML;
}
echo "</table>";
Related
This is my code with my output
<?php
$data = mysql_query("select * from request, users where request.user_id = users.id and request.user_id = $user_id") or die(mysql_error());
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr class='my_loans_tr'>";
print "<td class='detail'>" .$info['loan_id'] . "</td> ";
Print " <td class='admin_amount'>".$info['amount'] . "</td> ";
Print " <td class='admin_points'>".$info['points'] . "</td> ";
Print " <td class='admin_date'>".$info['req_date'] . " </td>";
Print " <td class='admin_status'>".$info['status'] . " </td>";
Print " <td class='admin_cancelled'>".$info['cancelled_loan'] . " </td></tr>";
Print "</tr>";
}
Print "</table>";
?>
output
The loan id is within the database, there is a second table named collected, that has info for each loan id (3 rows minimum of info based on same loan id)
How can I get each id from the query, click on it, get the current id and declare it as a value so I can execute another query which is,
select * from collected where loan id = "$loan_id clicked";
I have searched for javascript and jquery but I am not able to get it working with the post method, what is the best way to do this?
I am trying to get all values shown in a table from mysql but getting one .
I want to get the rows of the mysql table at in the last table mentioned in the code
////////Here is a desc of no use --- blah for just posting this question / as i am getting an error msg for giving more details information about this question /////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Here is the code:
$sql = 'SELECT
item_added
FROM
products_added
ORDER BY id';
$results = mysqli_query($conn, $sql);
if(mysqli_num_rows($results) < 1){
echo "No items";
}else{
$new_sql = 'SELECT
item_added,
quantity,
amount,
sum(amount) as items_total
FROM
products_added
where `username` = "'.mysqli_real_escape_string($conn, $_SERVER["REMOTE_ADDR"]).'"
ORDER BY id';
$resu = mysqli_query($conn, $new_sql);
}
?>
<table>
<thead>
<tr>
<td>Item</td>
<td>Qyt</td>
<td>Price</td>
<td>Action</td>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_assoc($resu)){
echo "<tr>";
echo "<td>" . $row['item_added'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
echo "<td>" . $row['amount'] . "</td>";
echo "<td><a class=\"remove-from-cart\" href=\"\"><i class=\"fa fa-times\"></i></a></td>";
echo "</tr>";
}
?>
</tbody>
</table>
It looks like its because you're using an aggregate function SUM() without a GROUP BY. In the $new_sql query, try adding "GROUP BY item_added" right before "ORDER BY id".
I want to count all user records and display them in tables, I am trying this code code, It displays the record for one user only, I want to display records from all users.
$u=$_POST['userid'];
$result1 = mysqli_query($con,"SELECT COUNT(user_id) as total FROM table-name where user_id=$u");
echo "<table border='1'>
</tr>
<tr>
<th>User ID</th>
<th>count</th>
</tr>";
while($row = mysqli_fetch_array($result1))
{
echo "<tr>";
echo "<td>" . $u . "</td>";
echo "<td>" . $row['total'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
Try the following SQL Query:
SELECT `user_id`, COUNT(`user_id`) as `total` FROM `table-name` GROUP BY `user_id`;
Refer to the documentation of the GROUP BY clause.
Use below:
$result1 = mysqli_query($con,"SELECT COUNT(user_id) as total FROM table-name");
where clause use for filter the data.
refer http://www.w3schools.com/sql/sql_where.asp
I am working on a formula where I am adding multiple values in a row and column depending on the values of other cells. For example I have the following working code:
$result = mysql_query("SELECT School,SUM(Wins + Losses) as total FROM tennis GROUP BY School");
echo "<table border='1'>
<tr>
<th>School</th>
<th>Score</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['School'] . "</td>";
echo "<td>" . $row['total'] . "</td>";
echo "</tr>";
}
However I want to add more columns that are also sums of other rows/columns and don't know how to do this while still keeping everything Grouped by the column 'School'. What I essentially want is the following, but the code is incorrect:
$result = mysql_query("SELECT School,SUM(Wins + Losses) as 1sttotal FROM tennis WHERE Event='1st Singles', SUM(Wins + Losses) as 2ndtotal FROM tennis WHERE Event='2nd Singles' GROUP BY School");
echo "<table border='1'>
<tr>
<th>School</th>
<th>1st Singles</th>
<th>2nd Singles</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['School'] . "</td>";
echo "<td>" . $row['1sttotal'] . "</td>";
echo "<td>" . $row['2ndtotal'] . "</td>";
echo "</tr>";
}
I'm new to PHP so I'm not sure the correct/optimal way to go about setting this up. Thanks
this is the query you should use to get the 2 totals:
SELECT
School,
SUM(IF(Event='1st Singles', Wins + Losses, 0)) as 1sttotal,
SUM(IF(Event='2nd Singles', Wins + Losses, 0)) as 2ndtotal
FROM tennis
GROUP BY School;
See how it adds according to the event columns? The trick is to pass a WHERE filter within the SELECT clause through conditional execution (IF)
Other possibility using CASE WHEN:
SELECT
School,
SUM(
CASE
WHEN Event='1st Singles' THEN Wins + Losses
ELSE 0
END
) as 1sttotal,
SUM(
CASE
WHEN Event='2nd Singles' THEN Wins + Losses
ELSE 0
END
) as 2ndtotal
FROM tennis
GROUP BY School;
I have the following code which works fine however historically and again now I want to (if possible) add a third select statement to collect further information and return it in my while loop i.e. as $row2.
Is there any way I can do this or will I have to rehash my code completely to make it work?
Any help or guidance will be gratefully received. Current code is as below:
mysql_select_db("jbsrint", $con);
$result = mysql_query("SELECT *, DATE_FORMAT(Date_Registered, '%d-%m-%Y') AS Date_Registered, DATE_FORMAT(Date_Last_MOT, '%d-%m-%Y') AS Date_Last_MOT, DATE_FORMAT(Date_Last_Tax, '%d-%m-%Y') AS Date_Last_Tax FROM Veh_List ORDER BY ID DESC");
$DUE_DATES = mysql_query("SELECT *, DATE_FORMAT(MOT_DUE_DATE, '%d-%m-%Y') AS MOT_DUE_DATE, DATE_FORMAT(Date_Tax_Due, '%d-%m-%Y') AS Date_Tax_Due FROM due_dates ORDER BY ID DESC");
echo "<table border='1'>
<tr>
<th>Vehicle Reg</th>
<th>Vehicle Make</th>
<th>Vehicle Model</th>
<th>Vehicle Colour</th>
<th>Date Registered</th>
<th>Date Of Last MOT</th>
<th>MOT Due</th>
<th>Date Of Last Tax</th>
<th>Tax Due</th>
<th>Vehicle Driver</th>
<th>Vehicle Driver Tel</th>
</tr>";
while($row = mysql_fetch_array($result) and $row1 = mysql_fetch_array($DUE_DATES))
{
echo "<tr>";
echo "<td class=\"td1\">" . $row['Vehicle_Reg'] . "</td>";
echo "<td >" . $row['Vehicle_Make'] . "</td>";
echo "<td class=\"td1\">" . $row['Vehicle_Model'] . "</td>";
echo "<td>" . $row['Vehicle_Colour'] . "</td>";
echo "<td class=\"td1\">" . $row['Date_Registered'] . "</td>";
echo "<td>" . $row['Date_Last_MOT'] . "</td>";
echo "<td class=\"td1\">" . $row1['MOT_DUE_DATE'] . "</td>";
echo "<td>" . $row['Date_Last_Tax'] . "</td>";
echo "<td class=\"td1\">" . $row1['Date_Tax_Due'] . "</td>";
echo "<td>" . $row['Vehicle_Driver'] . "</td>";
echo "<td class=\"td1\">" . $row['Vehicle_Driver_Tel'] . "</td>";
}
echo "</tr>";
echo "</table>";
mysql_close($con);
?>
You should not use a separate query for each table. Instead you should use JOIN to retrieve related data from multiple tables in a single query:
SELECT
veh_List.col1,
veh_List.col2,
due_dates.col3,
...etc...
FROM veh_List
JOIN due_dates ON veh_List.id = due_dates.id
-- add more joins here if you need data from more tables
ORDER BY veh_List.id DESC
You should learn about the JOIN keyword and the various possibilities this provides, including understanding the difference between INNER JOIN and OUTER JOIN.
What is the difference between "INNER JOIN" and "OUTER JOIN"?
I believe you can simply add another statement and combine it in the same fashion as you had now but my suggestion is to use the opportunity that you are modifying the code and merge the SQL queries into one.
Also, looking at your database design - is there one-to-one relation between the due dates and registered cars? If so, why not merge the two tables together? (Of course, I can only see the columns you are using in your select queries).
Furthermore, it seems a bit dangerous to rely on the ordering to match up the records - what if one row from due dates is removed?