I had one table listing events and attendies. To try Database normalization i split this into two tables.
I have made a database listing all the planned games for a team.
Table 1
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+name + organiser + date + location + cost + notes + id+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
i then have a second database that has everyone how has marked they will be comming
Table 2
++++++++++++++++++++++++++
+id + event + player +
++++++++++++++++++++++++++
The id is unique to each, The id from table 1 is used as the event in table 2.
i have a simple PDO query that pulls the data from table 1 into a HTMl table
if($db->connect_error) {
die("Connection error: ".$db->connect_error);
}
$sql = $db->query('SELECT * FROM event ORDER BY `date` ASC'
) or die($db->error);
echo"";
while($row = mysqli_fetch_assoc($sql))
{
$a = $row['attendees'];//will look up attendies to tick a check box if already aknowledged
$b = htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8');
if (strpos($a,$b) !== false) {
$c = "checked='checked'";
}else{
$c = "";
}
$r=$row['id'];
echo "<div id='results'>";
echo "<CENTER>";
echo "<table BORDER=6 class='fixed'>";
echo "<TR> ";
echo "<TD COLSPAN=3 ALIGN=CENTER><form action='going.php' method='post' name='form".$r."'>Event ".$row['name']." i'm going
<input type='checkbox' name='going'".$c." onclick='document.form".$r.".submit ();'>
<input type='text' name='Organise' value='".$r."'>
<input type='text' name='name' value='".$b."'>
</form></TD>";
echo "</TR> ";
echo "<TR>";
echo "<td>";
echo "<B><u><font color='#0080FF'>Title </font></b></u>".$row['name']."<br></font>";
echo "<B><u><font color='#0080FF'>Orginiser </font></b></u>".$row['organiser']."<br></font>";
echo "<B><u><font color='#0080FF'>When </font></b></u>".date("D j-M-Y GA",$row['dt'])."<br></font>";
echo "<B><u><font color='#0080FF'>Location </font></b></u>".$row['location']."<br></font>";
echo "<B><u><font color='#0080FF'>Cost </font></b></u>£".$row['cost']."<br></font></TD>";
echo "<TD ROWSPAN=3 valign='top'><B><u><font color='#0080FF'>Attendies </font></b></u>".$row['attendees']."<br></font></TD>";//will change to table 2
echo "<TD ROWSPAN=3 valign='top'><B><u><font color='#0080FF'>notes </font></b></u>".$row['notes']."<br></font></TD>";
echo "</tr>";
echo "</table>";
echo "</CENTER>";
echo "</div>";
}
i have tried and joind these using
$sql = $db->query('SELECT t1.*, t2.event as t2event, t2.player as player
FROM `event` as t1
LEFT JOIN `going` as t2 on t1.id = t2.event
ORDER BY t1.`dt` ASC'
but all i got was a HTML table per event and per player. I'm sure its possible but can't work it out, can i create a html table from quering table 1 and add to attendies all those going from table 2 not just one or creating a result each one. ?
Related
I have 3 sql tables and I want to make 1 table in html
The tables are:
Norm (norm_id, name, description, cluster_cluster_id, orden_orden_id)
Cluster (cluster_id, cluster_name)
Orden (orden_id, orden_name)
The table must contain:
norm_id - norm_name - norm - description - cluster_name - orden_name
I think i need to do this with a left outer join?
And how to show it in a table?
Right now i have
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['norm_id'] . "</td>";
echo "<td>" . $row['norm_name'] . "</td>";
echo "<td>" . $row['description'] . "</td>";
echo "<td>" . $row['cluster_name'] . "</td>";
echo "<td>" . $row['orden_name'] . "</td>";
echo "</tr>";
}
You can make left outer join like, SELECT column-names
FROM table-name1 LEFT JOIN table-name2
ON column-name1 = column-name2
WHERE condition
I have a BusinessID in both my staff and business table and I'm wanting to display the staff members for everyone in a particular business. The query below gives me this error.
ERROR: Could not able to execute SELECT * FROM business b inner join BusinessID b ON b.BusinessID = s.BusinessID WHERE b.BusinessID = 1. Not unique table/alias: 'b'
This is my foreign key file
<html>
<body>
<?php
include_once("connect.php");
$BusinessID = $_GET['BusinessID'];
$sql= "SELECT *
FROM business b
inner join BusinessID b
ON b.BusinessID = s.BusinessID
WHERE b.BusinessID = $BusinessID";
if($result = $conn->query($sql)){
if($result->num_rows > 0){
echo "<table>";
echo "<tr>";
echo "<th>Name</th>";
echo "<th>BusinessID<th>";
echo "</tr>";
while($row = $result->fetch_array()){
echo "<tr>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['BusinessID'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
$result->free();
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . $conn->error;
}
// Close connection
$conn->close();
?>
</body>
</html>
Below is the fix
$sql= "SELECT *
FROM staff
WHERE BusinessID = $BusinessID";
You need to use a join statement
Select *
From staff as S
Join business as B
on s.businessID=b.businessID
--Where clause <--- If you want to filter by anything.
You say you "have a BusinessID". If it were in $BusinessID, the form of query you want seems to be:
query select * from staff where s.BusinessID = $BusinessID
However be warned of SQL code injection as addressed at How can I prevent SQL injection in PHP? (See also the coincidental meta post from today Should the answer be the simplest ever possible, even at the expense of quality/security?) The correct way to deal with this in a PHP context is to query via prepared statements.
I'm joining 5 tables in vTiger in order to get all the info I need. However, there is an option that certain columns will be empty. In that case, my SELECT statement fails and I can't retrieve the rest of the results. How do I bypass this by adding a "Doesn't exist" default value if the column is blank?
$results = mysql_query("SELECT
vtiger_potentialscf.potentialid,
vtiger_potential.potentialname,
vtiger_contactdetails.accountid,
vtiger_contactdetails.salutation,
vtiger_contactdetails.firstname,
vtiger_contactdetails.lastname,
vtiger_account.accountname,
vtiger_crmentity.smownerid,
vtiger_crmentity.crmid,
vtiger_crmentity.label,
vtiger_users.id,
vtiger_users.email1
FROM vtiger_potential
INNER JOIN vtiger_potentialscf ON vtiger_potentialscf.potentialid = vtiger_potential.potentialid
INNER JOIN vtiger_crmentity ON vtiger_potential.potentialid = vtiger_crmentity.crmid
INNER JOIN vtiger_users ON vtiger_crmentity.smownerid = vtiger_users.id
INNER JOIN vtiger_contactdetails ON vtiger_potential.related_to = vtiger_contactdetails.accountid
INNER JOIN vtiger_account ON vtiger_account.accountid = vtiger_potential.related_to
WHERE `cf_919` = DATE(NOW())");
while ($row = mysql_fetch_assoc($results)) {
echo $row['email1'] . "<br />";
echo $row['smownerid'] . "<br />";
echo $row['potentialname'] . "<br />";
echo $row['accountid'] . "<br />";
echo $row['salutation'] . "<br />";
echo $row['firstname'] . "<br />";
echo $row['lastname'] . "<br />";
echo $row['accountname'] . "<br />";
In this case, I can have the vtiger_contactdetails empty. Any suggestion on how to get the rest of the content and just echo that these aren't available?
You could use left join.
Have a look http://www.w3schools.com/sql/sql_join_left.asp
Thanks Abhik Chakraborty for a much better explanation in the comments.
I have a table ORDERS and a table MASTERBASE.
Now I would like to output everything from the table Orders
AND
the data that corresponds to the booknumber(s) for each order.
There can be several booknumbers for each ORDER
The code only returns 1 order with book details, but there are several orders ..
<?
require "../LAB MANU/DbConnect.php";
mysql_query('SET NAMES UTF8');
$raw_results = mysql_query("SELECT * FROM ORDERS WHERE id > 0");
$num_rows = mysql_num_rows($raw_results);
if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
while($results = mysql_fetch_array($raw_results)){
echo $results ["id"];
echo "<br /><br />";
echo $results ["firstname"];
echo "<br /><br />";
echo $results ["name"];
echo "<br /><br />";
$ids = $results ["booknumbers"];
require "../LAB MANU/DbConnect.php";
mysql_query('SET NAMES UTF8');
$raw_results = mysql_query("SELECT * FROM MASTERBASE_VOLLEDIG WHERE BOEKNUMMER IN ({$ids})");
$num_rows = mysql_num_rows($raw_results);
if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
while($results = mysql_fetch_array($raw_results)){
// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
echo $results ["BESCHRIJVING"];
}}}}
?>
I Think you should change variable names
$raw_results to another one.Because this variable get a new value inside the loop.
I am trying to change the date format before it is displayed with SQL query however the date format is being completely ignored.
my code is
$query = "SELECT * , DATE_FORMAT(formatted, '%d/%m/%Y') from movies;";
then further down this is my table
echo "<table>"
echo "<table border='2'>"
echo "<tr>
<th>id</th>
<th>title</th>
<th>date</th>
</tr>";
while($row = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['formatted'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
this query is working, however the date format is being ignored and just displaying the date in yyyy-mm-dd I want it in DD-MM-YY.
thanks
Use an alias to name your calculated column
SELECT * , DATE_FORMAT(datetime, '%d/%m/%Y') AS formatted_date
from movies
Use a different name than the existing column to differ between the two. Then use
echo "<td>" . $row['formatted_date'] . "</td>";
to get the formatted one.
You need to mention the alias for the formatted datetime column other wise formatted value will not be called in your code
SELECT * ,
DATE_FORMAT(`datetime`, '%d/%m/%Y') `datetime`
from movies