This is my sql query
$sql = "SELECT comments.comment, users.userid, comments.bpid
FROM comments, users ,blogpages
WHERE comments.user_id = users.user and comments.bpid ='".$blogid."'
ORDER BY comments.cid;"
$query = mysqli_query($con,$sql) or die (mysqli_error($con));
while($row = mysqli_fetch_array($query,MYSQLI_ASSOC)){
$comm =$row["comment"];
$commentsuser =$row["userid"];
$commentbody .= "".$commentsuser."".$comm." ";
}
This is my form and display
<form action="" method="POST">
<textarea name="commentarea"></textarea>
<input type="submit" value="COMMENT" >
<h2><?php echo $commentbody; ?></h2>
</form>
but after i enter comment each value is displaying multiple times ?
the comment doesn't show up until i re-enter the page
You are missing a join condition:
SELECT comments.comment, users.userid, comments.bpid
FROM comments, users ,blogpages
WHERE comments.user_id = users.user AND
comments.bpid = blogpages.id AND -- Missing in the OP
comments.bpid ='".$blogid."'
ORDER BY comments.cid;
Having said that, implicit joins are deprecated, and you should probably be using an explicit join:
SELECT comments.comment, users.userid, comments.bpid
FROM comments
JOIN users ON comments.user_id = users.user
JOIN blogpages ON comments.bpid = blogpages.id
WHERE comments.bpid ='".$blogid."'
ORDER BY comments.cid;
This will also help visualize the join condition you were missing, since in this form its an actual part of the syntax.
I think the sql you are looking for is something like..
SELECT
comments.comment,
users.userid,
comments.bpid
FROM
comments,
INNER JOIN users ON comments.userId = users.userid,
INNER JOIN blogpages ON comments.bpid = blogpages.bpid
WHERE
comments.bpid ='".$blogid."'
ORDER BY
comments.cid;
Related
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.
So I have two tables, one storing the username and the other with their personal info. What I'm trying to get is a result where, I want to take the current active username and join it with the personal info table to get the ID, Username + their basic info to appear in a table. I came up with the query after googling, but it seems that I did something wrong. Can someone tell me what I did wrong?
users = the table that contain username
userinfo table with their basic info
$username is basically a variable that was stored in the $SESSION
<?php
$query = mysql_query("
SELECT users.UserID, users.Username, userinfo.FirstName, userinfo.LastName,
FROM users
WHERE users.Username = $username
INNER JOIN userinfo
ON users.UserID=userinfo.UserID");
?>
Assuming I have the parse data to table coding right below that specific code, what is wrong with the query?
Basically the error I keep getting is error #1064 at line #2 (when I run the query without the php code). Thanks in advance!
<?php
$query = mysql_query("
SELECT users.UserID, users.Username, userinfo.FirstName, userinfo.LastName
FROM users
INNER JOIN userinfo
ON users.UserID=userinfo.UserID
WHERE users.Username = $username
");
?>
Try this
<?php
$query = mysql_query("
SELECT users.UserID, users.Username, userinfo.FirstName, userinfo.LastName,
FROM users
INNER JOIN userinfo
ON users.UserID=userinfo.UserID
WHERE users.Username = $username");
?>
The syntax is -
SELECT col, ...
FROM <tbl_name> AS t1
INNER JOIN <join_tbl_name> AS t2
ON t1.col = t2.col
WHERE <cond>
See the below code. If you print it, you will find where error occurs.
echo $query = mysql_query("SELECT users.UserID, users.Username, userinfo.FirstName, userinfo.LastName FROM users WHERE users.Username = '$username' INNER JOIN userinfo ON users.UserID=userinfo.UserID");
or use
$sql="SELECT users.UserID, users.Username, userinfo.FirstName, userinfo.LastName FROM users WHERE users.Username = '$username' INNER JOIN userinfo ON users.UserID=userinfo.UserID";
if(!mysql_query($sql,$con))
{
echo "Unexpected Error <br/>";;
die (mysql_error());
}
where $con is mysql connection statement.
You have an extra comma at the end of field select. Try removing that part:
$query = "SELECT users.UserID, users.Username, userinfo.FirstName, userinfo.LastName
FROM users
INNER JOIN userinfo
ON users.UserID=userinfo.UserID
WHERE users.Username = $username";
I have a system where I getting images out of my database, but when it does that, there is 3x of the same images.
I have tried with different ways, DISTINCT and such, but I have no clue how I fix this.
Here is my query code:
<?php
$id = $_GET['id'];
$query = "SELECT DISTINCT * FROM billeder INNER JOIN album ON fk_album_ID = $id";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_assoc($result))
{
$thumb_src = 'billeder/thumb_'.$row['billeder_sti'];
$full_src = 'billeder/'.$row['billeder_sti'];
echo "
<div class='ikon'>
<a href='$full_src'>
<img src='$thumb_src' alt='' />
</a>
</div>
";
}
?>
Hope someone can help me on the way to fix this :)
Without being able to see your table structure I won't be able to give an exact answer but the likely reason is because your INNER JOIN is not setup correctly.
SELECT DISTINCT *
FROM billeder
INNER JOIN album
ON (billeder.fk_album_ID = album.pk_album_ID)
WHERE
billeder.fk_album_ID = $id
Something like the above would be the correct way to JOIN a table and using a WHERE clause to then limit the date received.
JOIN must be used with two tables columns. See example:
SELECT * FROM tableA a INNER JOIN tableB b ON a.id = b.a_id;
What you're trying to make is something like this:
"SELECT DISTINCT * FROM billeder INNER JOIN album ON
billeder.fk_album_ID = album.album_id WHERE billeder.id = $id"
You shouldn't pass an argument to the JOIN. The arguments must be used on the WHERE clause.
i have this piece of code that selects all users from the table and another sql statement that counts the number of records for every user. the problem I'm facing is that i have a sql in a foreach loop which is not good for performance but i wasn't able to combine both of them in one statement. any advise?
$query = $db->getAll("SELECT * FROM users");
foreach($query as $v){
$tpl->setCurrentBlock('useri');
$query2 = $db->numRows("SELECT * FROM signups AS s INNER JOIN users AS u ON s.userid=u.id WHERE u.id={$v['id']}");
$tpl->setVariable('total',$query2);
$tpl->setVariable($v);
$tpl->parseCurrentBlock();
}
Try this query against your DB:
SELECT u.id, COUNT(s.*)
FROM users u
LEFT JOIN signups s ON s.userid = u.id
GROUP BY u.id
I hope I got it right. I have no SQL DB to test it right here. Important: You have to group by each field you select that is no aggregate.
Edit:
If it is not fast enough yet, an index on signups.userid may help. This is hypothetic, however, so you should check the Execution Plan your query engine generates.
$query = $db->getAll("
SELECT u.id, u.name, COUNT(*) total
FROM signups AS s RIGHT JOIN users AS u ON s.userid=u.id
GROUP BY u.id, u.name
ORDER BY u.name
");
foreach($query as $v){
$tpl->setCurrentBlock('useri');
$tpl->setVariable('total', $query['total']);
// ...
$tpl->parseCurrentBlock();
}
If I understand good your problem, initialize variable and in loop increase it.
int i;
foreach($query as $v){
$tpl->setCurrentBlock('useri');
$query2 = $db->numRows("SELECT * FROM signups AS s INNER JOIN users AS u ON s.userid=u.id WHERE u.id={$v['id']}");
$tpl->setVariable('total',$query2);
$tpl->setVariable($v);
$tpl->parseCurrentBlock();
i++;
}
ok here is my php and mysql code:
where it is bold i wanted to the the uid from the online table and if it in there
where online.uid = '' i needed so it put the uid in there.
$sql = ("select accounts.id,
accounts.tgid,
accounts.lastactivity,
cometchat_status.message,
cometchat_status.status,
**online.uid**
from friends_list join accounts
on friends_list.fid = accounts.id
left join cometchat_status
on accounts.id = cometchat_status.userid
where friends_list.status = '1'
and **online.uid = ''**
and friends_list.uid = '".mysql_real_escape_string($userid)."'
order by tgid asc");
#sledge identifies the problem in his comment above (I'm not sure why he didn't post an answer).
You are selecting a column from the online table, but you don't include it in your FROM clause. You have to query from a table in order to reference its columns in other parts of the query. For example:
$sql = ("select accounts.id,
accounts.tgid,
accounts.lastactivity,
cometchat_status.message,
cometchat_status.status,
online.uid
from friends_list
join accounts on friends_list.fid = accounts.id
join online on ( ??? )
left join cometchat_status
on accounts.id = cometchat_status.userid
where friends_list.status = '1'
and online.uid = ''
and friends_list.uid = '".mysql_real_escape_string($userid)."'
order by tgid asc");
You need to fill in the join condition, because there's not enough information in your original post to infer how the online table is related to other tables.
PS: Kudos for using mysql_real_escape_string().