Connect PHP pages in dreamweaver to MySQL queries - php

I'm trying to connect a series of five select menus created on Dreamweaver to generate one result through this query below. I keep getting errors every time I run the query. This is the query I have on my PHP page:
enter code here
<?php
$connect= mysql_connect("localhost","root", "password");
// Check connection
if (!mysql_select_db('db_name', $connect)) {
echo 'Could not select database';
exit;
}
$sql = "SELECT SER_ID and ser_type FROM services
INNER JOIN service_type ST
ON s.SER_ID= st.SER_ID
inner join profile P
on st.P_ID = p.P_ID
inner join room_services rs
on rs.SER_ID = s.SER_ID
inner join room r
on r.R_ID = rs.R_ID
inner join room_sensor rse
on rse.R_ID = r.R_ID
inner join sensor sen
on sen.S_ID = rse.S_ID
inner join service_sensor ss
on ss.SER_ID = s.SER_ID
inner join services_conditions sc
on sc.SER_ID = s.SER_ID
inner join conditions c
on c.C_ID = sc.C_ID
where p.username = Adam
AND sen.sensor_type = motion detector";
$result= mysql_query($sql, $connect);
echo "<table border='1'>
<tr>
<th>ser_ID</th>
<th>service_Type</th>
</tr>";
while($row = mysql_query($result)) {
echo "<tr>";
echo "<td>" . $row['ser_ID'] . "</td>";
echo "<td>" . $row['service_Type'] . "</td>";
echo "</tr>";
}
echo "</table>";
if (!$result) {
echo "DB Error, could not query the database\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
while ($row = mysql_fetch_assoc($result)) {
echo $row['ser_ID'];
echo $row['serice_Type'];
}
mysql_free_result($result);
mysql_close($connect);
?>
enter code here
enter code here
The error I keep receiving is:
DB Error, could not query the database MySQL Error: Query was empty
This is happening despite the fact that the query is running completely fine on phpMyAdmin.
Any help appreciated.

Related

Getting information from multiple using MySQLI and be able to echo results

for the past few hours, I have been trying to find a simple method of while loop echoing information from multiple tables at once. I'd like to say I've not pulled all my hair out looking, but I have.
Here is one mysqli query to get the following fields from CUSTOMER
$tid = $_SESSION['user_id']; // "id" is 1 for example
$query = "SELECT * FROM `CUSTOMER` WHERE user_id = {$tid}";
$results = mysqli_query($dbconnection, $query);
while ($row = mysqli_fetch_array($results)) {
echo $row['user_id'] . "<br><br>";
echo $row['c_fname'] . "<br>";
echo $row['c_sname'] . "<br>";
};
Here is another mysqli query to get the following fields from SALE
$query = "SELECT * FROM `SALE` WHERE user_id = {$tid}";
$results = mysqli_query($dbconnection, $query);
while ($row = mysqli_fetch_array($results)) {
echo $row['s_date'] . "<br>";
echo $row['s_total'] . "<br>";
};
Could someone possibly show me how I can get both of these tables in one query so that echoing both tables information is possible at the same time instead of separately. I am not fussed how it is done, As long as it gets all from both tables for echoing purpose, that is good.
You can do it by using LEFT JOIN like this.
SELECT column_name(s)
FROM table1
LEFT JOIN table2 ON table1.column_name = table2.column_name;
And this is your code.
$query = "SELECT * FROM `CUSTOMER` LEFT JOIN `SALE` ON `SALE`.user_id=`CUSTOMER`.user_id WHERE `SALE`.user_id={$tid}";
$results = mysqli_query($dbconnection, $query);
while ($row = mysqli_fetch_array($results)) {
echo $row['user_id'] . "<br><br>";
echo $row['c_fname'] . "<br>";
echo $row['c_sname'] . "<br>";
echo $row['s_date'] . "<br>";
echo $row['s_total'] . "<br>";
}
For more info read this,
https://www.w3schools.com/sql/sql_join_left.asp
I hope this helps.
EDITED
This is for joining 3 tables,
SELECT column_name(s)
FROM table1
LEFT JOIN table2 ON table1.column_name = table2.column_name;
LEFT JOIN table3 ON table1.column_name = table3.column_name;
In your code.
SELECT * FROM `CUSTOMER`
LEFT JOIN `SALE` ON `CUSTOMER`.user_id = `SALE`.user_id
LEFT JOIN `PRODUCTS` ON `CUSTOMER`.user_id = `PRODUCTS`.user_id
WHERE `SALE`.user_id={$tid};
As variable.
$query = "SELECT * FROM `CUSTOMER` LEFT JOIN `SALE` ON `CUSTOMER`.user_id = `SALE`.user_id LEFT JOIN `PRODUCTS` ON `CUSTOMER`.user_id = `PRODUCTS`.user_id WHERE `SALE`.user_id={$tid}";
You can use the following code and will help u solve Ur problem
$query = "SELECT C.*,S.* FROM CUSTOMER C,SALES S
WHERE C.user_id={$tid}
and C.user_id=S.user_id;
while ($row = mysqli_fetch_array($results)) {
echo $row['C.user_id'] . "<br><br>";
echo $row['C.c_fname'] . "<br>";
echo $row['C.c_sname'] . "<br>";
echo $row['S.s_date'] . "<br>";
echo $row['S.s_total'] . "<br>";
};
You can simply join the tables to get your expected result as shown below.
$query = "SELECT c.user_id, c.c_fname, c.c_sname, s.s_date, s.s_total FROM `CUSTOMER` AS c INNER JOIN `SALE` AS s ON c.user_id = s.user_id WHERE c.user_id = {$tid}";
Joining 3 tables example
$query = "SELECT *
FROM `CUSTOMER` AS c
INNER JOIN `SALE` AS s ON c.user_id = s.user_id
INNER JOIN `PRODUCTS` AS p ON p.product_id = s.product_id
WHERE c.user_id = {$tid}";

PHP, MySQL - JOINs

I am new to PHP and I just cannot figure out my code. I am using MySQL and PHP.
table: person
PK: personID
Other fields: lastName, firstName, hireDate, imgName
table: validMajors
PK: majorAbbrev
Other Fields: majorDesc
(Junction) table: personMajors
personID, majorAbbrev
When I run my code (using NATURAL JOIN) it will display the image, last&first name, and hire date. Which is great! But I need it to display their majors as well (I would like the majorAbbrev to be displayed). It also does not display people who are in the person table but are not in the personMajors table, which is an issue because we have staff members in the person table (who do not have a major since they are not a student)
Here is my code:
<table align="center">
<?php
$connection = mysqli_connect(DBHOST, DBUSER, DBPASS, DBNAME);
if ( mysqli_connect_errno() ) {
die( mysqli_connect_error() );
}
$sql = "SELECT * FROM person NATURAL JOIN personMajors ORDER BY lastName";
if ($result = mysqli_query($connection, $sql)) {
// loop through the data
$columns=4;
$i = 0;
while($row = mysqli_fetch_assoc($result))
{
if($i % $columns ==0){
echo "<tr>";
}
echo "<td class='staffImage badgeText frameImage displayInLine'>" . "<img src='images/staff/".$row['imgName'].".jpg'>". "<br>".
"<strong>" . $row['firstName'] . "</strong>" ." ".
"<strong>" . $row['lastName'] . "</strong>" . "<br>" .
"Hire Date: ".$row['hireDate'] ."</td>";
"Major: " .$row['majorAbbrev'] ."</td>"; //Does not display
if($i % $columns == ($columns - 1)){
echo "</tr>";
}
$i++;
}
// release the memory used by the result set
mysqli_free_result($result);
}
// close the database connection
mysqli_close($connection);
?>
</table>
Any ideas/solution will be greatly appreciated!
Because you are not concatenating your php properly. You ended (;) your echo after displaying the $row["lastName"].
You can try these to join the three tables:
SELECT * FROM person
LEFT JOIN personMajors ON person.personID = personMajors.personID
LEFT JOIN validMajors ON personMajors.majorAbbrev = validMajors.majorAbbrev
Or you can define what columns to call in your query:
SELECT person.personID,
person.lastName,
person.firstName,
person.hireDate,
person.imgName,
validMajors.majorAbbrev,
validMajors.majorDesc
FROM person
LEFT JOIN personMajors ON person.personID = personMajors.personID
LEFT JOIN validMajors ON personMajors.majorAbbrev = validMajors.majorAbbrev
Then you can call the results with the way you are calling it right now (cleaner version):
echo '<td class="staffImage badgeText frameImage displayInLine">
<img src="images/staff/'.$row["imgName"].'.jpg"><br>
<strong>'.$row["firstName"].'</strong>
<strong>'.$row["lastName"].'</strong><br>
Hire Date: '.$row["hireDate"].'
Major: '.$row["majorAbbrev"].'
</td>';
(Second try): Is the person to major relationship one to one or one to many?
OK, this SELECT Statement should work:
SELECT person.*, validMajors.* FROM person AS p, validMajors AS vm, personMajors AS pm WHERE p.personID = pm.personID AND pm.majorAbbrev = vm.majorAbbrev

How to echo values from another table?

Hello I have 2 tables named tbl_guard and tbl_records. The tbl_guard has columns guard_id and fullname, while tbl_records has guard_id also. And here is my code:
$query = "SELECT * FROM tbl_records";
$stmt = $dbc->prepare($query);
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
extract($row);
echo "<table>";
echo "<tr>";
echo "<td>Name</td>";
echo "</tr>";
echo "<tr>";
echo "{$guard_id}";
echo "</tr>";
}
echo "</table>";
What I want to do is instead of the guard_id being echoed there in the loop, I want the fullname in the tbl_guard table.
Your help would be very much appreciated!
Use JOIN
$query = "SELECT tbl_records.*, tbl_guard.fullname FROM tbl_records LEFT
JOIN tbl_guard ON tbl_guard.guard_id = tbl_records.guard_id";
In PHP
while($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
.
.
.
echo "{$fullname}";
}
What you want to achieve seems to be a foreign key relation - you have to use JOINS. In your example:
SELECT r.*, g.fullname FROM tbl_records r LEFT JOIN tbl_guard g ON r.`guard_id`=g.`guard_id`
Read MySQL documentation, you will need this more often!
I got it working already, here is my sql query:
SELECT tbl_records.*, tbl_residents.fname, tbl_residents.lname FROM tbl_records LEFT JOIN tbl_residents ON tbl_residents.stud_id = tbl_records.stud_id
Thanks everyone!

2 columns on a left join

Hello so I have 2 tables. tbl_records and tbl_guards. On tbl_guards I have guard_id and on tbl_records I have guard_id and guard_id_in. And here is my current code:
try
{
$stat = "0";
$query = "SELECT rec.*, tbl_guard.fname, tbl_guard.lname
FROM tbl_records as rec
LEFT JOIN tbl_guard ON tbl_guard.guard_id = rec.guard_id
LEFT JOIN tbl_records ON tbl_records.guard_id_in = tbl_guard.guard_id
WHERE rec.stud_id=? AND rec.status=?";
$stmt = $dbc->prepare($query);
$stmt->bindParam(1, $_GET['id']);
$stmt->bindParam(2, $stat);
$stmt->execute();
echo "<table cellpadding='3' class='searchTbl'>";
echo "<thead>";
echo "<tr>";
echo "<th>Actual Date</th>";
echo "<th>Purpose</th>";
echo "<th>Destination</th>";
echo "<th>Exact TO</th>";
echo "<th>Expected TI</th>";
echo "<th>Guard</th>";
echo "<th>Actual TI</th>";
echo "<th>Guard IN</th>";
echo "</tr>";
echo "</thead>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
extract($row);
$guard = $fname . " " . $lname;
echo "<tbody>";
echo "<tr>";
echo "<td>$act_date</td>";
echo "<td>$purpose</td>";
echo "<td>$destination</td>";
echo "<td>$exact_timeout</td>";
echo "<td>$exp_timein</td>";
echo "<td>$guard</td>";
echo "<td>$act_timein</td>";
echo "<td>$guard</td>";
echo "</tr>";
echo "</tbody>";
}
}
catch (PDOException $e)
{
echo "Error: " . $e->getMessage();
}
echo "</table>";
Here is tbl_records data.
And here is tbl_guard data.
Here is the current output.
My problem is it shows the same guard in guard_id and guard_id_in in my code.
You can use LEFT JOIN multiple times, like:
SELECT tbl_records.*, tbl_guard.fname, tbl_guard.lname
FROM tbl_records as rec
LEFT JOIN tbl_guard ON tbl_guard.guard_id = rec.guard_id
LEFT JOIN tbl_records ON tbl_records.guard_id_in = tbl_guard.guard_id
You can use:
SELECT tbl_records.*, tbl_guard.fname, tbl_guard.lname
FROM tbl_records
LEFT JOIN tbl_guard ON (tbl_records.guard_id OR tbl_records.guard_id_in) = tbl_guard.guard_id
You should select twice the guard table and use aliases for your fields :
SELECT tr1.*, tg1.fname AS FNAME, tg1.lname AS LNAME,
tg2.fname AS FNAME_IN, tg2.lname AS LNAME_IN
FROM tbl_records AS tr1
LEFT JOIN tbl_guard AS tg1
ON tg1.guard_id = tr1.guard_id,
LEFT JOIN tbl_guard AS tg2
ON tg2.guard_id = trl.guard_id_in
then in PHP you'll have more vars : $FNAME, $LNAME, $FNAME_IN, $LNAME_IN
Just to be sure, your tbl_records table can have a link to two different tbl_guards via guard_id and guard_id_in ?
Maybe you can try :
SELECT tbl_records.*, tbl_guard.fname, tbl_guard.lname FROM tbl_records LEFT JOIN tbl_guard ON tbl_guard.guard_id IN (tbl_records.guard_id, tbl_records.guard_id_in)
SELECT tr1.*, tg1.fname, tg2.lname
FROM tbl_records AS tr1
LEFT JOIN tbl_guard AS tg1 ON tg1.guard_id = tr1.guard_id,
LEFT JOIN tbl_guard AS tg2 ON tg2.guard_id = trl.guard_id_in

Where Clause Does Not Seem To Be Working

I have a inner join statment which is working 90% as it is displaying what I need it to display but unfortunately it is not displaying to the right user logged in. It is being displayed to whoever logs into my system. Here is the code:
<?php
$result = mysql_query("SELECT * FROM Agendas INNER JOIN Meetings ON Meetings.secretary WHERE Agendas.approval = 'disapproved' AND secretary = '". $_SESSION['username']."'")
or die(mysql_error()); ;
if (mysql_num_rows($result) == 0) {
echo 'You Have No New Messages';
} else {
while($info = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td><br/>" .'Title: '. $info['title']." </td>";
echo "<td><br/>" .'Approved: '. $info['approval']. "</td>";
echo "<td><br/>" .'Reason: '. $info['reason']."</td>";
echo "<hr>";
}
}
echo "</tr>";
echo "</table>";
?>
my database tables look like this:
Meetings: meeting_id, title, chairperson, secretary, tof, occurances, action
Agendas: agenda_id, subject, duration, meeting_id, approval, reason.
thanks soo much for any help :)
i think you're doing something wrong in the join clause. how about this:
$result = mysql_query("SELECT * FROM Agendas INNER JOIN Meetings ON Agendas.meeting_id = Meetings.meeting_id WHERE Agendas.approval = 'disapproved' AND secretary = '". $_SESSION['username']."'")
or die(mysql_error());

Categories