PHP joined MySQL query - php

I am working on a comments system in which i will want to pull information from both tables, the problem is the results from the first query (the ID which is found) effects the output of the second query. My tables look like this.
members
Id, First_name, Last_name, Email, Password, Img_url, Activation_No, Activated, and Date
posts
PostId, Email, Text, ForumId, DatePosted, Likes, Dislikes
The code i am using is;
<?php
// retrive post
include('php/config.php');
include ('php/function.php');
// retrive comments with post id
$stmt = $mysqli->prepare("SELECT (posts.Email,posts.Text) FROM posts WHERE posts.ForumId = '$forumId' LEFT JOIN SELECT (members.Img_url) FROM members WHERE members.Email = (posts.Email)");
$stmt->execute();
$stmt->bind_result($PostId,$Email,$Text,$ForumId,$DatePosted,$Likes,$Dislikes,$usr_img);
$stmt->fetch();
print "<div class=comment-item>";
print "<div class=comment-avatar>";
print "<img src=".$usr_img."alt=avatar>";
print "</div>";
print "<div class=comment-post>";
print "<h4>" .$Email. "<span> said....</span></h4>";
print "<p>" .$Text. "</p>";
print "</div>";
print"</div>";
$stmt->close();
?>
The Error message i am receiving is;
Fatal error: Call to a member function execute() on a non-object in C:\Users\PC\Documents\XAMPP\htdocs\post.php on line 90

You have an error on your SQL query.
Try this
SELECT posts.Email, posts.Text, members.Img_url
FROM posts
LEFT JOIN members
ON members.Email = posts.Email
WHERE posts.ForumId = '$forumId'

Your prepare() query is failing, so $stmt is a non-object. It looks like your query syntax is wrong as you can't have a LEFT JOIN after WHERE, and JOIN uses ON not WHERE. Try something like this-
$stmt = $mysqli->prepare("SELECT posts.Email,posts.Text, members.Img_url FROM posts LEFT JOIN members ON members.Email = posts.Email WHERE posts.ForumId = '$forumId'") ;

SELECT posts.Email, posts.Text, members.Img_url
FROM posts
LEFT JOIN members ON members.Email = posts.Email
WHERE posts.ForumId = '$forumId'
You should really check out the syntaxes of mysql.. You made a few mistakes here ;)
I hope it works like this.

Related

Notice: Undefined index: comment_author

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.

Get friends profile picture(s) from user table

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)

php mysql select something and get columns from other tables

I'm using prepared statements and I need to "select" other table, apart from these two, to get data but I get this:
Fatal error: Call to a member function bind_param() on a non-object in C:\xampp\htdocs\views\user\referral.php on line 16
If I add in SELECT table1.* , table.* , "theothertable.*"
$stmt = $mysqli->prepare("SELECT friends.*, rc_usuario.* // or just *
FROM friends
INNER JOIN rc_usuario ON rc_usuario.id = friends.friendID
WHERE friends.userID = ?");
$stmt->bind_param('s', $connectedUserID);
This is working fine, I get what i need, but I also need to get data from another table and I can't make other select because i need it all in a while to print all the data together.
The question is, can I SELECT something like that from 2 tables and also get data from other table/s?
Thank YOU!
EDIT: Add the new statement:
if ($stmt = $mysqli->prepare("SELECT friends.*, members.*, account_type.*
FROM friends
INNER JOIN members ON members.id = friends.friendID
INNER JOIN account_type ON account_type.name = members.acc_type
WHERE friends.userID = ? AND members.acc_type = ?")) {
$stmt->bind_param('is', $connectedUserID, $connectedAcc_type);
$stmt->execute();
} else echo $mysqli->error;
You can join more tables by using another INNER JOIN, like as follows;
INNER JOIN rc_usuario ON rc_usuario.id = friends.friendID
INNER JOIN rc_another ON rc_another.col = friends.coljoin
Just make sure you select all the columns you want in the joined table.
It might also help to run your prepare statement in an if, like this;
if($stmt = $mysqli->prepare("SELECT ...")) { // ... where the rest of your query is
$stmt->bind_param('s', $connectedUserID);
$stmt->execute();
}
else {
echo $mysqli->error;
}
which will give you an idea of any problems with the SQL syntax.
Hope this helps.

Getting information from 3 tables SQL & PHP

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");

Query works in mysqlyog query window but not in php script?

I have two tables I am selecting from them 1 I want to get the user information and 2 I want to get all images belonging to the user . but the query does not retrieve the images but in query window I get them . Also in the script if I decide to select from the images table the images are displayed but when I do the joining stuff it does not work. I know there is something am not doing well . please any help will be appreciated
Bellow is the code
$query='SELECT
tish_clientinfo.lastname, tish_clientinfo.address,
tish_clientinfo.firstname,
tish_images.image_name
FROM tish_clientinfo
INNER JOIN tish_images
ON tish_clientinfo.user_id = tish_images.user_id
WHERE user_id= '. intval($_GET['user_id']);
$result = $con->prepare($query);
$result->execute();
May be the server got confused about user_id. Try this
$query='SELECT
tish_clientinfo.lastname, tish_clientinfo.address,
tish_clientinfo.firstname,
tish_images.image_name
FROM tish_clientinfo
INNER JOIN tish_images
ON tish_clientinfo.user_id = tish_images.user_id
WHERE tish_clientinfo.user_id= '. intval($_GET['user_id']);
You just having a little problem just after the where clause on the user_id specify the table just like I have done in the bellow answer
$query='SELECT
tish_clientinfo.lastname, tish_clientinfo.address,
tish_clientinfo.firstname,
tish_images.image_name
FROM tish_clientinfo
INNER JOIN tish_images
ON tish_clientinfo.user_id = tish_images.user_id
WHERE tish_clientinfo.user_id= '. intval($_GET['user_id']);
$result = $con->prepare($query);
$result->execute();

Categories