i have two tables: USERS(id, name, email, password) QUESTIONS(id, userid, type, ques, date, time)
i have to pass the value of questions.id as qid. i am unable to get the desired result with the following code. instead of taking 'id' value of QUESTIONS table, it is taking the 'id' value of USERS table, and therby producing wrong output.
$query = "
SELECT *
, q.id qid
FROM questions q
JOIN users u
ON u.id = q.userid
WHERE type = 'technical'
ORDER
BY date
, time DESC
";
$result = mysqli_query($conn,$query);
while ($row = mysqli_fetch_array($result)) {
$id = $_SESSION['id'];
$ques=$row['question'];
$user = $row['username'];
echo strtoupper($user);
echo "<a href='allview.php?qid=$row[id]' class='class4'> $ques </a>";
}
?>
This code:
$id = $_SESSION['id'];
Refers to the id
column in the select
. That id
comes from the users table.
You have renamed the question id to qid
, so try:
$qid = $_SESSION['qid'];
A word of advice: only fetch the columns you are actually going to use:
SELECT u.username, q.id as qid
FROM questions q JOIN
users u
ON u.id = q.userid
WHERE type = 'technical'
ORDER BY date, time DESC;
The *
is handy when debugging, but the code could end up behaving unexpectedly at some point because the underlying table structure changes.
Try to use this code
$query = "
SELECT *
, q.id AS qid,
q.id AS id
FROM questions q
Try using this
$query = "
SELECT *
, questions.id qid
FROM questions q
, users u
WHERE q.userid = u.id
AND type = 'technical'
ORDER
BY date
, time DESC
";
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.