Inner join in mysql, syntax error, regarding php query - php

$threadID=$_GET['threadID'];
$result=mysql_query("
SELECT * FROM threads AS Threads
INNER JOIN users AS Users ON Threads.user_id=Users.user_id
WHERE thread_id='$threadID' LIMIT 1
") or die(mysql_error());
I get this:
u have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near ''11' at
line 3
I wrote many inner joins before. Why is my syntax wrong near the end of the query
updated: 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 '12' LIMIT 1' at line 3

First of all, your query is WIDE open to sql injection attacks.
Second, to figure out why you get the syntax error, make the query building phase separate from the actual query call:
$sql = "SELECT ....";
echo $sql;
$result = mysql_query($sql) or die(...);
This way you can see the entire query. MySQL's error messages only report the portion of the query from where it thinks the error is onwards, but sometimes it decides wrong and elminates the actual relevant part where th error is... so... examine the ENTIRE query.

ah, may be I know the answer.
There is a thing in mysql, i believe, called "strict mode" or something, which being too picky about data types.
Try to make your query like this
$id = (int)$_GET['threadID'];
$sql = "SELECT * FROM threads t INNER JOIN users u ON t.user_id=u.user_id
WHERE thread_id=$id LIMIT 1";
$res = mysql_query($sql) or trigger_error(mysql_error().$sql);

Why not join your query in this fashion:
$threadID = mysql_real_escape_string( $_REQUEST['threadID'] ); // Get some cleanup in there...
$result=mysql_query("SELECT threads.*, users.*
FROM threads, users
WHERE threads.user_id = users.user_id
AND threads.thread_id = '$threadID'
LIMIT 1
") or die( mysql_error() );

Related

ReadBeanPHP SQL query doesnt go throught. Does anybody see a mistake? Did everything by manual and it doesnt work

Here is my SQL - query with ReadBeanPHP.
$idItem = $this->route['alias']; //some $_GET value
$product = \R::findMulti('listings, users',
'SELECT listings.*, users.*
RIGHT JOIN listings.id_user = users.id_user
WHERE listings.id = ? AND listings.status = 0', [$idItem]);
Here is a mistake what it prints me out
Code error Exception
Text error SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'JOIN listings.id_user = users.id_user WHERE listings.id = 4 AND lis' at line 2
Where error /opt/lampp/htdocs/sellbuyschool42/vendor/gabordemooij/redbean/RedBeanPHP/Driver/RPDO.php
Line error 194
I did everything as it's written on official web-site of ReadBeanPHP https://redbeanphp.com/index.php?p=/finding
I want to get couple so-call beans, but getting error. Or if anybody know how easy to combine two tables with ReadBeanPHP that would work to. Providing pics of tables as well.
Your join statement is wrong, there is no from and even you have not declare which table you want to do a right join. you can try the following.
SELECT
l.*,
u.*
from listings l
RIGHT JOIN users u
on l.id_user = u.id_user
WHERE l.id = ?
AND l.status = 0

Why it shows me error when preparing a sentence in PHP with MySQLi

I'm creating a sentence prepared in PHP, and I run into a rare syntax error, I do not know if it is breaching any of MySQL or why I show that error
The syntax is as follows, I want to sort by row and by ascending or descending type and limit the results
$query = "SELECT * FROM myTable ORDER BY ? ? LIMIT? ,?"
if($conn->prepare($query)){ .. } // error
The error is
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 '? LIMIT ? , ?' at line 1
If you execute said statement in MySQL, it correctly throws the results
Parameters to ORDER BY are not values, and cannot be parametrised. One is a column reference, the other is a keyword.
For example do like this and try.
$query = "SELECT * FROM myTable ORDER BY column_name LIMIT 0,10";

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 ''')

Am pretty new to PHP and stuch with this problem.
Am trying to insert some info from a dropdown menu into a database but keep getting this error. Please help if you can.
The php code is:
<select name="group_ID">
<?php
$pos_query ="SELECT groupName, group_ID FROM 'operations' JOIN members WHERE operations.group_ID = members.group_ID";
$pos_results = $db->query($pos_query);
for ( $i=0; $i < $pos_results->num_rows ; $i++ )
{
$pos_row = $pos_results->fetch_assoc();
echo'<option value"'.$pos_row['group_ID'].'">';
echo $pos_row['groupName'].'</option>';
}
?></select></td>
I have a table called operations and trying to join it to another table called members and insert the data into the members table.
The error is:
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 ''') SELECT 'Church Elders' FROM operations JOIN members ON ope' at line 1.
Sorry guys I posted the wrong code.
The actual code is:
$query = "INSERT INTO members(email, name, gender, dob, profile, password,)
SELECT $group_ID
FROM operations
JOIN members
WHERE operations.group_ID = '$group_ID'" ;
$result = $db->query($query);
Am trying to insert email, name, gender... group_ID into the members table and getting the group_ID from the operations table and gets the following error message:
Error Inserting Details. Error Message:
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 ') SELECT Church Elders FROM operations JOIN members WHERE oper' at line 1.
Please help!!!!
No need to use single quote with table name('operations') & specify table name with row like groupName.operations, group_ID.operations.
You probably meant to use backticks in your SQL query, not single-quotes. Also, you probably meant the ON statement, rather than WHERE. WHERE is for filtering, while ON tells the JOIN statement which columns to join on.
SELECT `groupName`, `group_ID` FROM `operations` JOIN `members` ON `operations`.`group_ID` = `members`.`group_ID`

Why doesn't this MySQL select query work with WHERE clause?

Hello this is driving me crazy. I can't get this query to work with the WHERE clause. It works without it. I have tried everything. I have looked at dozens of websites and dozens of questions here. I can't see anything wrong with this query. But it gives me this error whenever I try to use a WHERE clause:
1064: 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 'WHERE `cond` = '1'' at line 4
I have tried it with spaces and without, with single quotes and without, I have tried mysqli. I just can't figure out what the problem is.
Here is the code I currently have:
$sql = <<<SQL
SELECT *
FROM `master_inv`
ORDER BY `sku`
WHERE `cond` = '1'
SQL;
WHERE clause goes before ORDER BY.
$sql = <<<SQL
SELECT *
FROM `master_inv`
WHERE `cond` = '1'
ORDER BY `sku`
SQL;
Your SQL statement is out of order and your missing a select statement.
The ORDER BY statement should be placed after the WHERE clause
SELECT *
FROM master_inv
WHERE cond = '1'
ORDER BY sku
It is just the order which is incorrect try:
SELECT *
FROM `master_inv`
WHERE `cond` = '1'
ORDER BY `sku`

You have an error in your SQL syntax? Works on its own, but get error when doubled up

Bit puzzled by this, when i comment out the second query i get no error, the first query runs fine but when the second one (basicly the same query) is added i get you have an error in your sql syntax.
Little bit unsure why, any help is appreciated maybe there is a better way for me to do the query, thanks.
SQL ERROR [ mysqli ]
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 '' at line 1 [1064]
SQL
SELECT username FROM forum_users WHERE user_id =
BACKTRACE
$challengingClan = $results->get('challengingClan');
$query = "SELECT username FROM forum_users WHERE user_id = $challengingClan";
$user1 = $db->sql_query($query);
$user1 = $db->sql_fetchrow($user1);
$opposingClan = $results->get('opposingClan');
$query = "SELECT username FROM forum_users WHERE user_id = $opposingClan";
$user2 = $db->sql_query($query);
$user2 = $db->sql_fetchrow($user2);
It is likely that $results->get('opposingClan') is not returning anything. This should be easy to debug :-)
it is because you are telling the variables $results and $query to be two different things change one of them

Categories