php mysql select something and get columns from other tables - php

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.

Related

The above query will not pull the information from the database

$read = "SELECT * FROM elmtree
WHERE id ='$getid' AND
INNER JOIN elmtree_users ON elmtree.userid = elmtree_users.id";
The above query will not pull the information from the database to publish to the website.
Im trying to pull the item from the database with the $getid but also join the item id with the userid who uploaded it. Then using a while loop to print out the item to screen.
Any help would be greatly appreciated.
The SQL syntax you show isn't correct. A join clause describes which tables will be available for the rest of the query; all tables you mention need to be part of the ‘FROM’ clause, so that is where the ‘JOIN’ also belongs.
SELECT *
FROM elmtree
INNER JOIN elmtree_users ON elmtree.userid = elmtree_users.id
WHERE id ='$getid'
Your SQL query was not correctly written. The AND is not used in joining tables and the WHERE statement should be after the JOIN.
Try the following:
$read = "SELECT * FROM elmtree INNER JOIN elmtree_users ON(elmtree.userid = elmtree_users.id) WHERE elmtree.id ='$getid'";
UPDATE
Could you try the following in your code (replacing $this->database with your database variable) and post the result:
if ($result = $conn->query($read)) {
while ($row = $result->fetch_assoc()) {
...
}
} else {
throw new Exception("Database Error [{$this->database->errno}] {$this->database->error}");
}
$read = "SELECT * FROM elmtree INNER JOIN elmtree_users ON(elmtree.userid = elmtree_users.id) WHERE elmtree.itemid ='$getid'
Big thanks to Omari Celestine to finding the answer to the problem!

PHP, How to display the 2nd table

DBOperations.php
public function getByQRID($id){
$stmt = $this->conn->prepare("SELECT students.*, courses.* FROM students INNER JOIN courses ON courses.id = students.course_id WHERE students.id = ?");
$stmt->bind_param("s", $id);
$stmt->execute();
return $stmt->get_result()->fetch_assoc();
}
Update the code
If you are you using MySql, then try this:
select * from Student
inner join Course on Student.course_id = Course.id
where Student.id = 1
(1 it's an example, and I'm guessing things, since your question wasn't entirely clear).
I hope it helps!

Multiple mysql table joins with php?

Honestly I have no idea how to do what I am trying to do. I am not overly experienced in php nor in Mysql but I am trying and could use some help, preferably with working example code.
Problem: I have 3 tables
members
customfields
customvals
members contains:
membername | Id
customfields contains:
rank | name
customvals contains
fieldid | userid | fieldvalue
Table columns match at
customvals.userid=members.id
customvals.fieldid=members.rank
What I need to do is match the data so that when page.php?user=membername is called it displays on the page
Table1.membername:<br>
Table2.name[0] - Table3.fieldvalue[0]<br>
Table2.name[1] - Table3.fieldvalue[1]<br>
etc...
(obviously displaying only the information for the said membername)
The more working the code, the more helpful it is for me. Please don't just post the inner join statements. Also it is most helpful to me if you could explain how and why your solution works
So far here is what I have for code:
$profileinfocall = "SELECT Table1.`membername`, Table2.`name`, Table3.`fieldvalue`
FROM members AS Table1
LEFT JOIN customvals AS Table3 ON Table1.`id` = Table3.`userid`
LEFT JOIN customfields AS Table2 ON Table3.`fieldid` = Table2.`rank`
WHERE Table1.`membername` = $username;";
$membercall = "SELECT * FROM members WHERE membername=$username";
$profileinfo = mysql_query($profileinfocall, $membercall);
while($row = mysql_fetch_array($profileinfo)) {
echo $row['membername'];
}
Obviously this doesn't work as I get the following errors:
Warning: mysql_query() expects parameter 2 to be resource, string given on line 534.
Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in on line 535
While this is a very broad question and you have not provided any PHP code, you might want to break it down into various sections:
Establishing a connection to the database (with mysqli) and sending a query:
$c = mysqli_connect("localhost","user","password","db");
if (mysqli_connect_errno())
echo "Failed to connect to MySQL: " . mysqli_connect_error();
else {
$result = mysqli_query($c,"SELECT * FROM members");
while($row = mysqli_fetch_assoc($result)) {
echo "{$row['membername']}";
}
}
mysqli_close($c);
Tieing your tables together:
It is better to start off with a clear structure (including line breaks) when getting into the MySQL syntax. One way would be to have some sort of query skeleton:
SELECT tablealias.column, table2alias.field3
FROM table AS tablealias
LEFT|RIGHT|INNER JOIN table2 AS table2alias ON table.id=table2.id
WHERE (this and that = true or false, LIKE and so on...)
Breaking it down to your specific problem this would be:
SELECT Table1.`membername`, Table2.`name`, Table3.`fieldvalue`
FROM members AS Table1
LEFT JOIN customvals AS Table3 ON Table1.`id` = Table3.`userid`
LEFT JOIN customfields AS Table2 ON Table3.`fieldid` = Table2.`rank`
WHERE Table1.`Id` = 'UserID to be searched for'
Improvements & Security measures:
But there is even more to it than meets the eye. If you have just begun, you might as well dive directly into prepared mysqli- statements. Given the query to get your members, the only changing part is the ID. This can be used for a prepared statement which is much more secure than our first query (though not as fast). Consider the following code:
$sql = "SELECT Table1.`membername`, Table2.`name`, Table3.`fieldvalue`
FROM members AS Table1
LEFT JOIN customvals AS Table3 ON Table1.`id` = Table3.`userid`
LEFT JOIN customfields AS Table2 ON Table3.`fieldid` = Table2.`rank`
WHERE (Table1.`Id` = ?)";
$c = mysqli_connect("localhost","user","password","db");
$stmt = $c->stmt_init();
if ($stmt->prepare($sql)) {
$stmt->bind_params("i", $userid);
$stmt->execute();
while ($stmt->fetch()) {
//do stuff with the data
}
$stmt->close();
}
$mysqli->close();
This SQL query should do it:
SELECT a.membername, a.Id, b.fieldid, b.userid, b.fieldvalue, c.rank, c.name
FROM members AS a
LEFT JOIN customvals AS b ON a.id = b.userid
LEFT JOIN customfields AS c ON b.rank = c.fieldid
WHERE a.Id = #MEMBERIDHERE#;

PHP joined MySQL query

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.

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