i need to show support tickets from the table tickets against user_id from users table. I'm using MySQLI. Here is the query that i have written and that is not working for me:
SELECT user_id, subject, message FROM tickets
INNER JOIN users.user_id WHERE user_id='".$user_id."'"
Can anyone please correct this query?
Full code here
<?php
$servername = "localhost";
$username = "d";
$password = "ddds";
$dbname = "sddd";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT user_id, subject, message FROM tickets INNER JOIN users.user_id USING (user_id) WHERE user_id='".$user_id."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table class='table table-bordered'>
<thead>
<tr>
<th>Subject</th>
<th>Date</th>
<th>Status</th>
<th>View</th>
</tr>
</thead>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["subject"]."</td><td>".$row["date"]." ".$row["status"]."</td><td>".$row["view"]."</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
Corrected syntax:
SELECT T.user_id, T.subject, message FROM tickets T
INNER JOIN users U ON U.user_id = T.user_id WHERE `T.user_id='".$user_id."'"`
You have forgot ON
.
If you want to use INNER JOIN
you have to specify where you are making a join (using on
or using
) like these fallowing queries :
SELECT user_id, subject, message
FROM tickets
INNER JOIN users.user_id ON tickets.user_id = users.user_id
WHERE user_id = '".$user_id."'"
OR
SELECT user_id, subject, message
FROM tickets
INNER JOIN users.user_id USING (user_id)
WHERE user_id = '".$user_id."'"
you should join only tables and not fields!
SELECT user_id, subject, message FROM tickets
INNER JOIN users.user_id WHERE user_id='".$user_id."'"
join tickets
table to users.user_id
that is a field of users
table. Should be:
SELECT user_id, subject, message FROM tickets
INNER JOIN users USING (user_id) WHERE user_id='".$user_id."'"
You can also use ON to do the join (using is a shortcut when the colum names match):
SELECT user_id, subject, message FROM tickets
INNER JOIN users ON tickets.user_id=users.user_id WHERE user_id='".$user_id."'"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.