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");
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 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.
I have 2 table's:
Users (id, username, email, avatar, etc...);
Friends (id, user1, user2, status);
Now I want to build on my profile page an list of my friends with there avatar(s). I'm trying for like 4 hours by myself but i don't get it... :(
BTW: this is an error i got!
Notice: Array to string conversion in /home/reduaqi158/domains/reduankurtaj.eu/public_html/snapfriends/vrienden.php on line 26
This is what i have right now:
<?php
error_reporting(E_ALL);
session_start();
$username = $_SESSION['username'];
$status = 2;
include "includes/conn.php";
$vrienden=mysqli_query($server,"SELECT * FROM vrienden WHERE status='$status' && vriend1='$username' || vriend2='$username' ");
$vriend_list = array();
while($row = mysqli_fetch_array($vrienden))
{
if ($row['vriend1'] == $username) {
$vriend_list[] = $row['vriend2'];
}
else {
$vriend_list[] = $row['vriend1'];
}
}
echo json_encode($vriend_list);
$foto=mysqli_query($server,"SELECT prof_pic FROM users WHERE username='$vriend_list['vriend1''vriend2']' ");
while($row2 = mysqli_fetch_array($foto)) {
echo "<img class='img-rounded' src=assets/profiel/".$row2['prof_pic']." alt='Card image cap'>";
}
?>
json_encode output:
["ja","amando"]
Someone who can help me pls :)
Your initial approach is very confusing.
Almost everything in your code can be substituted by single SQL query.
You can use JOIN to get all your friends with their avatars in one go:
SELECT u.username as username, u.avatar as avatar,.... <== all columns which you need
FROM `friends_table` f <== your friends table
JOIN `users_table` u <== your users table
ON (f.user1 = u.id) <== notice that i join on user1 column
WHERE u.username = '$username' && f.status = '$status'
UNION
SELECT u.username as username, u.avatar as avatar,.... <== same columns
FROM `friends_table` f <== your friends table
JOIN `users_table` u <== your users table
ON (f.user2 = u.id) <== notice that i join on user2 column
WHERE u.username = '$username' && f.status = '$status'
By this query you select all users who are in a friendship with your $username. You need union because you don't know in which field (user1 or user2) your $username is located.
NOTE: I strongly suggest using prepared statements instead of just putting '$var' inside SQL query to prevent SQL Injection.
After executing this query you can parse results and display avatars in such a way:
while($row = mysqli_fetch_array($vrienden, MYSQLI_ASSOC))
{
echo "<img class='img-rounded' src=assets/profiel/".$row['avatar']." alt='Card image cap'>";
}
I hope you got the idea.
in your while statement you have to declare a value for the array. like array[0] = value. so that you know that array position 0 has a certain value. Like what I did here below. Don't know if it's in PHP like this but certain in .net you have to declare the location of a value in an array.
while($row = mysqli_fetch_array($vrienden))
{
if ($row['vriend1'] == $username) {
$vriend_list[0] = $row['vriend2'];
}
else {
$vriend_list[1] = $row['vriend1'];
}
}
and the following
$foto=mysqli_query($server,"SELECT prof_pic FROM users WHERE username='$vriend_list['vriend1''vriend2']' ");
shouldn't it be $vriend_list['vriend1'] . $vriend_list['vriend2']'
you have to use a connect character (the . in PHP)
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";
can somebody help me?
here is my php script...
$req = mysql_query('SELECT id, email, start_date, end_date, time_event, time_submitted, payment_method, status FROM booking_members WHERE status="Canceled"');
while($dnn = mysql_fetch_array($req))
$req1 = mysql_query('DELETE FROM booking_members WHERE id='$id');
while($dnn1 = mysql_fetch_array($req1))
{
<td class="left"><center>
<img src="images/cross.png"></img>
</td></center>
how can i get the id from $dnn['id'] in order to delete rows?
Why not just run the delete directly?
DELETE FROM booking_members WHERE status='Cancelled'
There is absolutely no reason to first select all the id's and then loop through them all deleting rows one at a time.
Mike Brant is exactly right; You should delete them without doing a lookup first. But if you wanted to access the id, you'd use:
// ... PREVIOUS
while ($dnn = mysql_fetch_array($req)) {
$delete_id = $dnn['id'];
$req1 = mysql_query("DELETE FROM booking_members WHERE id='".$delete_id."'");
// ... CONTINUE
while($dnn = mysql_fetch_array($req))
Where is the { ?
And here:
$req1 = mysql_query('DELETE FROM booking_members WHERE id='$id');
If you want the id, of last query, you must do: $dnn['id'] and not $id ( or you set $id before)...
And, for include a HTML script, in PHP script, you must use echo or print or must close the tags php ( ?> )
If you want to do a look up, and im assuming you are using dot net nuke, download the free reports module. After installing it edit it and type in the following select statement:
Select u.id,
u.username,
u.email,
b.start_date,
b.end_date,
b.time_event,
b.time_submitted,
b.payment_method,
b.status
FROM users as u
Join
booking_members as b on u.userid = b.userid
Where status="Canceled"'
this will show you the username, email, start_date, End_date, Time_event, Time_submitted, Payment_method and status of users where there status is canceled...