I tried many things from StackOverflow regarding this error but with no luck.
I try to show a reply from a comment. The reply comment is shown along with the date and hour when posted but the author who posted the reply is not shown.
The page with error is comments.php on the line $com_name = $row['comment_author'];
The code is:
<?php
$get_id = $_GET['post_id'];
$get_com = "select * from comments where post_id='$get_id'";
$run_com = mysqli_query($con, $get_com);
while($row=mysqli_fetch_array($run_com)){
$com = $row['comment'];
$com_name = $row['comment_author'];
$date = $row['date'];
echo "
<div id='comment'>
<h3>$com_name</h3><i>Said</i> on $date
<p>$com</p>
</div>
";
}
?>
Hope to sort out this error.
Thank you and best regards.
It looks like there are no column with name comment_author. But there are field user_id. You need to modify you query to below (assume user table users has fields user_name and user_id)
$get_com = "select c.date, c.comment, u.user_name as comment_author
from comments c inner join users u on c.user_id = u.user_id
where c.post_id='$get_id'";
The answer is simple. The column_author column simply does not exist in your comments table. You probably need to join the comments table with the table that actually contains the data you need.
Example:
SELECT A.*, B.`name` as comment_author FROM `comments` A
LEFT JOIN `users` B ON A.`user_id` = B.`id`
WHERE A.`post_id`='$get_id'
Remember to always properly escape your SQL queries.
Related
I have a simple blog where I'm practicing some php and mysql. I'm trying to display the username of the post author (you know where it says posted by Author).
I have two tables blog_members and blog_posts which are related 1 to many so I got a memberID field into the blog_posts. I'm trying to get the username of the member who's the author of the post.
I was thinking to do a join or something but I can't figure this out.
Here's what I was trying to do but it's not working because I'm sure I'm not using it properly.
$query1 = "SELECT username from blog_members JOIN blog_posts ON memberID = memberID ";
$result1 = mysqli_query($link, $query1);
$row1 = mysqli_fetch_array($result1);
PS: I got it working one way by using SESSION to get the userID but that works only if the user is logged is which is not the case, I want to display the name in any case.
Thanks!
Use inner join this way
And with a proper sanitize use $your_user_id for match
$query1 = "SELECT username
from blog_members
INNER JOIN blog_posts ON blog_members.memberID = blog_posts.memberID
WHERE blog_posts.memberID = '" .$your_user_id . "';";
JOIN syntax is wrong in your query.
Use following query:
$query1 = "SELECT username from blog_members JOIN blog_posts ON blog_members.memberID = blog_posts.memberID ";
$result1 = mysqli_query($link, $query1);
$row1 = mysqli_fetch_array($result1);
Try something like this, usng INNER JOIN :
$query1 = "SELECT blog_members.username FROM blog_members INNER JOIN blog_posts ON blog_members.memberID = blog_posts.memberID ";
reference : http://www.w3schools.com/sql/sql_join.asp
Here is a solution using a simple WHERE condition (same performance Explicit vs implicit SQL joins) :
SELECT a.username FROM blog_members a, blog_posts b WHERE a.memberID = b.memberID
But if you need more information about MySQL Join : https://www.sitepoint.com/understanding-sql-joins-mysql-database/
Hope this helps !
I have 2 tables, one for comments and one for the thread. They are both connected with the thread_id which checks what comments belong to what thread. Now I got a profile page and I want to show the comments posted by the user (which already works) and to what thread(column title) each comment belongs to. I am a beginner with cross table queries and I was wondering how I could accomplish this. Here is my code for showing the comments posted by the user:
$sql_result4 = $mysqli2->query("SELECT * FROM comments WHERE username = '".$profileusername."'");
while($postcomments = mysqli_fetch_assoc($sql_result4)){
echo"<a href='thread.php?thread_id={$postcomments['comment_id']}'>{$postcomments['comment']}</a></br>";
}
I was thinking about something like this:
$sql_result4 = $mysqli2->query("SELECT * FROM comments WHERE username = '".$profileusername."'");
$sql_result5 = $mysqli2->query("")
while($postthread = mysqli_fetch_assoc($sql_result5){
while($postcomments = mysqli_fetch_assoc($sql_result4)){
echo"<a href='thread.php?thread_id={$postcomments['comment_id']}'>{$postcomments['comment']}</a></br>";
}
}
but I have no idea what to use for $sql_result5
What should I put as $sql_result5
You can use just 1 query and use left join.
SELECT c.*, t.* FROM comments c
LEFT JOIN threads t ON t.id = c.thread_id
WHERE c.username = 'username'
Now you have all data in 1 result.
I am working on a messaging system for one of my sites and I'm having a lot of issues with getting the messages to show up. For some reason I can't get the syntax right so that it will take every single value that matches the receiver_id will display if the user_id matches the receiver_id. I had it working fine on my windows test machine, but when I uploaded it to my ubuntu server it broke. So now I'm trying to fix the code. What I have is this:
$m = "SELECT m.message_id, m.receiver_id, m.sender_id, m.subject, m.body, m.posted, u.id, u.username FROM messages m, users u WHERE u.id = {$_SESSION['id']}";
$b = mysqli_query($dbc, $m);
?>
<div class="mailbox">
Inbox<br />
Sent<br />
</div>
<div class="mailbox">
<h2>Messages for <?php echo $_SESSION['username']; ?></h2>
<?php echo output_message($message); ?>
<div id="messages">
<?php
if (mysqli_num_rows($b) > 0) {
while ($mess_row = mysqli_fetch_array($b, MYSQLI_NUM)) {
$u = "SELECT * FROM users WHERE id={$mess_row[1]}";
$rec = mysqli_query($dbc, $u);
$rec_row = mysqli_fetch_array($rec, MYSQLI_NUM);
$s = "SELECT * FROM users WHERE id={$mess_row[2]}";
$send = mysqli_query($dbc, $r);
$send_row = mysqli_fetch_array($send, MYSQLI_NUM);
echo "$mess_row[3] Posted by: <b>$send_row[1]</b> on: $mess_row[5]<br /><hr />";
}
} else {
echo "No messages.";
}
What am I doing wrong? I thought I had it fixed but then it just spit out the same record five times. I tried grouping which only gave me 1 record. I tried moving the $s call outside the while loop but I'm not able to draw on the sender's user_id that way. I just don't know what to do and I've searched all over for the past several hours and I can't find exactly what I'm looking for, or else I'm not typing the question right.
Thanks in advance.
Use a JOIN to get everything in one query:
$m = "SELECT m.message_id, m.receiver_id, m.sender_id, m.subject, m.body, m.posted, s.username sender_name, r.username receiver_name
FROM messages m
JOIN users r ON r.id = m.receiver_id
JOIN users s ON s.id = m.sender_id
WHERE u.id = {$_SESSION['id']}";
I apologize for hastily throwing this together. Hope it helps.
Use this as your query, then you won't have to go through and do that horrible extra set of queries to get the user info.
$id = $dbc->real_escape_string($_SESSION['id']);
$m = "SELECT m.message_id, m.receiver_id, m.sender_id, m.subject, m.body, m.posted, u.id, u.username
FROM messages m
INNER JOIN users u ON m.receiver_id=u.id
WHERE u.id = '$id';";
Extra note: Your need to escape your session id string. I'd probably also verify it is just numbers using a regex.
edit: Use #Barmar answers query in place of mine. I'd still add the escaping/regex though.
Im making a blog like system using HTML, CSS, PHP and MySQl.
The site is made up of three tables.
user (id, username, password, email)
posts (postid, title, post)
comments (postid, id, comment, commentid) postid coming from posts and id from user.
I am trying to display all of the comments and the users username who left them for a certain post.
When i use this query in phpmyadmin:
SELECT user.username, comments.comment FROM user INNER JOIN comments on user.id=comments.id where postid=1
It shows what i need.
When i add it into php i get a blank page.
<?php
//echo "1";
session_start();
$connect = mysql_connect('localhost', 'root', 'root') or die("Couldn't connect");
mysql_select_db("com541blog") or die("Couldn't connect to database");
//echo "2";
//$postid = $_GET['type'];
$_SESSION['postid'] = $postid;
//echo "3";
$query_comments = mysql_query("SELECT user.username as username, comments.comment as comment FROM user INNER JOIN comments on user.id=comments.id WHERE postid='1'");
$info = mysql_fetch_array($query_comments);
$username = $info['username'];
$comment = $info['comment'];
echo $username;
echo $comment;
?>
Thanks in advance for the help :)
You're not executing any query.
$rs = mysql_query($query_comments);
$info = mysql_fetch_array($rs);
Your first line has an error I suspect, ie missing 'c' near the end of 'connect'.
include("db_connet.php"); should be include("db_connect.php");
Also, missing a semi-colon ;. This:
$query_comments = ("SELECT user.username, comments.comment
FROM user INNER JOIN comments on user.id=comments.id
where postid=1")
Should read:
$query_comments = ("SELECT user.username, comments.comment
FROM user INNER JOIN comments on user.id=comments.id
where postid=1");
Also, not bad practice to qualify each of your column names with a table name eg user.username as you're doing. But you might prefer eg the following more concise syntax using table aliases:
$query_comments = ("SELECT u.username, c.comment
FROM user u INNER JOIN comments c on u.id = c.id
where c.postid = 1");
(Note the table aliases don't need to be a single letter, so can be handy reducing a table name such as "ManufacturerSuppliedPartsListData_Feb01", to eg "mpl", without losing their meaning. Or eg if you've got "Customers" and "Credit" instead of just "c" you might use eg "cust" and "cred")
You need to specify mysql_query in PHP ... Else your query will not be executed
Like :
$query_comments = mysql_query("SELECT user.username, comments.comment FROM user INNER JOIN comments on user.id=comments.id where where postid=1");
<?php
include 'dbFunctions.php';
$courseid = $_GET['Course_id'];
$query = "SELECT * FROM course WHERE Course_id=".$courseid."";
$arrCourse = executeSelectQuery($query);
$query2 = "SELECT * FROM course_member WHERE Course_id=".$courseid."";
$result = mysqli_query($link,$query2) or die(mysqli_error($link));
?>
HTML body:
<?php
while ($row = mysqli_fetch_array($result)) {
?>
<?php echo $row['member_id']
?>
<?php
}
?>
this correctly prints the results of the member id that is involved in the selected course, however, i want to extract the first_name using the result generated that belong to another table called member.
been trying all the join queries but it didn't work.
Sounds like you need to read up on the JOIN operation in SQL. The following query will return all the data you seem to need, from a single query:
SELECT *
FROM course
INNER JOIN course_member ON course.id = course_member.course_id
INNER JOIN member ON member.id = course_member.member_id
As far as I see
$sql="select last_name from course_member
join member on course_member.member_id = member.member_id
where course_member.Course_id = ".$courseid;
I'm going to take a guess here at your table defs...
$query2 = "SELECT m.first_name, cm.* FROM course_member cm join member m on m.id = cm.member_id WHERE Course_id=".$courseid."";
If you want only the member name and you are not much interested with the id then you can merge it into a single query
$query = SELECT fName from member WHERE id = (SELECT member_id FROM course_member WHERE Course_id=$courseid);