I had the following query
$stmt2 = $con->prepare("SELECT `id`, `category_id`, `topic_id`, `post_creator`, `post_content`, `post_date` FROM forum_posts WHERE `category_id`=? AND `topic_id`=?");
I now am trying to join my users table, so that I can match the forum_posts.post_creator to users.id and get the username from that.
I am trying this, but the query keeps failing..
$stmt2 = $con->prepare("SELECT forum_posts.id, forum_posts.category_id, forum_posts.topic_id, forum_posts.post_creator, forum_posts.post_content, forum_postspost_date FROM forum_posts
WHERE forum_posts.category_id=? AND forum_posts.topic_id=?
INNER JOIN users
ON forum_posts.post_creator = users.id");
if ( !$stmt2 || $con->error ) {
// Check Errors for prepare
die('Stmt2 SELECT prepare() failed: ' . htmlspecialchars($con->error));
}
$stmt2->bind_param("ii", $cid, $tid);
if(!$stmt2->execute()) {
die('Stmt2 SELECT execute() failed: ' . htmlspecialchars($stmt2->error));
}
$stmt2->store_result();
$stmt2->bind_result($post_id, $post_category_id, $post_topic_id, $post_creator, $post_content, $post_date, $posts_username);
I get this error, but cannot figure out what is wrong with that section.
Stmt2 SELECT prepare() failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INNER JOIN users ON forum_posts.post_creator = users.id' at line 3
What am I doing wrong?
The inner join is part of the from clause, not part of the where clause:
SELECT forum_posts.id, forum_posts.category_id, forum_posts.topic_id, forum_posts.post_creator, forum_posts.post_content, forum_postspost_date
FROM forum_posts
INNER JOIN users
ON forum_posts.post_creator = users.id
WHERE forum_posts.category_id=? AND forum_posts.topic_id=?
Your prepared statement should be like this. JOIN should come before Where
("SELECT forum_posts.id, forum_posts.category_id, forum_posts.topic_id,
forum_posts.post_creator, forum_posts.post_content, forum_postspost_date
FROM forum_posts INNER JOIN users
ON forum_posts.post_creator = users.id
WHERE forum_posts.category_id=? AND
forum_posts.topic_id=?"
);
Related
I want to create a procedure that checks the data in 'Table1' against the data in 'Table2'. I have used this query to perform the procedure.
$sq_query = 'SELECT * FROM Table1 WHERE NOT EXISTS (SELECT * FROM Table2 WHERE Table1.name = Table2.name)';
$result = mysqli_query($conn, $sq_query);
if (!$result) {
die ('SQL Error: ' . mysqli_error($conn));
}
BTW, I want to find out any name which isn't in 'Table2' but is in 'Table1'. I have been unsuccessful in locating missing files so far.
You can try using left join
SELECT * FROM table1 left join
Table2 on Table1.name = Table2.name
where Table2.name is null
Please help me to check my query. I have search a lot and I have'nt try to select 3 tables before.
I think I got it right but I dont know why there's nothing happen.
public function delSection($delete_id)
{
$stmt = $this->conn->prepare("SELECT * FROM tbl_section
JOIN tbl_login ON (tbl_login.sec_id = tbl_section.sec_id)
JOIN tbl_content ON (tbl_content.sec_id = tbl_section.sec_id)
WHERE tbl_section.sec_id=:del_id");
$stmt->execute(array(":del_id"=>$delete_id));
while($linkRow=$stmt->fetch(PDO::FETCH_ASSOC))
{
unlink(__DIR__."/Admin/cover_images/".$linkRow['sec_cover']);
unlink(__DIR__."/Admin/Files/".$linkRow['sec_id']."/".$linkRow['file_name']);
rmdir(__DIR__."/Admin/Files/".$linkRow['sec_id']);
}
$stmt2 = $this->conn->prepare("DELETE tbl_section, tbl_login, tbl_content FROM tbl_section
JOIN tbl_login ON (tbl_login.sec_id = tbl_section.sec_id)
JOIN tbl_content ON (tbl_content.sec_id = tbl_section.sec_id)
WHERE tbl_section.sec_id=:del_id");
$stmt2->bindparam(":del_id",$delete_id);
$stmt2->execute();
return true;
}
What I am trying to do is to select * from 3 tables and fetch their data with fk sec_id
here's the manual running of query
link:
Code:
Done With LEFT OUTER JOIN QUERY
$stmt = $this->conn->prepare("SELECT * FROM tbl_section
LEFT OUTER JOIN tbl_login ON (tbl_login.sec_id = tbl_section.sec_id)
LEFT OUTER JOIN tbl_content ON (tbl_content.sec_id = tbl_section.sec_id)
WHERE tbl_section.sec_id=:unlink_id");
I have two tables 'lr_users' and 'lr_ranks' with the following table structures:
lr_users:
user_id(pk), username, rank(fk), job_id(fk), date_joined
lr_ranks: rank_id(pk), name
$qry= mysqli_query($con, "SELECT lr_users.username, lr_users.rank, lr_users.job_id, lr_users.date_joined lr_ranks.name AS rankname FROM lr_users
LEFT JOIN lr_ranks ON lr_users.rank = lr_ranks.rank_id")or die(mysqli_error($con));
$rows = mysqli_num_rows($qry);
Above is my PHP code for generating a 'LEFT JOIN' query, however when I run it i get the following error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.name AS rankname FROM lr_users LEFT JOIN lr_ranks ON lr_users.rank = lr_ran' at line 1.
What am I doing wrong?
Syntax error (comma missing before lr_users.date_joined):
$qry= mysqli_query($con, "SELECT lr_users.username, lr_users.rank, lr_users.job_id, lr_users.date_joined, lr_ranks.name AS rankname FROM lr_users
LEFT JOIN lr_ranks ON lr_users.rank = lr_ranks.rank_id")or die(mysqli_error($con));
$rows = mysqli_num_rows($qry);
If you formatted your code more legibly, this kind of error wouldn't arise:
$query = "
SELECT u.username
, u.rank
, u.job_id
, u.date_joined
, r.name rankname
FROM lr_users u
LEFT
JOIN lr_ranks r
ON u.rank = r.rank_id;
";
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.
I'm trying to make a MySQL select for use in a mysql_query function, but I keep getting this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LEFT JOIN Orgs ON Jobs.CustOrgID = Orgs.ID
LEFT JOIN Persons ON Jobs.CustPer' at line 11
I've tried everything and searched everywhere, but nothing seems to be working. All help is appreciated.
$Qry = "SELECT
Jobs.ID,
'Jobs.Status',
Jobs.JobNum,
'Orgs.Nme',
'Persons.FirstNme',
'Persons.LastNme',
'JobTypes.JobType',
'Jobs.Dsc',
'Jobs.Notes'
FROM Jobs ";
if($column !== null && $text !== null) {
$Qry .= "WHERE " . $column . " LIKE '%" . $text . "%' ";
}
$Qry .= "LEFT JOIN Orgs ON Jobs.CustOrgID = Orgs.ID
LEFT JOIN Persons ON Jobs.CustPersonID = Persons.ID
LEFT JOIN JobTypes ON Jobs.JobTypeID = JobTypes.ID
ORDER BY JobNum";
SOLUTION:
SELECT ...
FROM ...
LEFT JOIN ...
LEFT JOIN ...
WHERE ...
ORDER BY ...
My WHERE was in the wrong place, it should come after the two LEFT JOINS.
You're inserting the WHERE in the wrong place. It must come AFTER the joins:
SELECT ...
FROM ...
LEFT JOIN ...
LEFT JOIN ...
WHERE ...
ORDER BY ...
you should use backticks instead of single quotes , or you dont have to use backticks if they are not reserved keywords or separated strings .
try this :
$Qry = "SELECT
`Jobs`.`ID`,
`Jobs`.`Status`,
`Jobs`.`JobNum`,
`Orgs`.`Nme`,
`Persons`.`FirstNme`,
`Persons`.`LastNme`,
`JobTypes`.`JobType`,
`Jobs`.`Dsc`,
`Jobs`.`Notes`
FROM Jobs ";