Error in SQL Syntax while trying to convert database - php

I got an Error while trying to convert my database.
Fehler in 163 - 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 ')' at line 2
Query war : SELECT * FROM phpbb_users WHERE user_active = 1 AND user_id NOT IN ()
My Code:
$e0 = query("SELECT * FROM ".$phpbb_prefix."_users
WHERE user_active = 1 AND user_id NOT IN (".implode("','", $ilch_to_phpbb).")" , $phpbb_con)
or die('Fehler in '. __LINE__ . ' - '.mysql_errno($phpbb_con) . ' : '. mysql_error($phpbb_con).'<br /> Query war : ' . $lastquery . '<hr />');
I can not recognize the error. Any Ideas?
Database: MySQL(i) 5.5.53-0+deb7u1
PHP: 5

The problem is with your implode. Imagine if $ilch_to_phpbb contains 2 user id's, the query will be:
SELECT * FROM ".$phpbb_prefix."_users
WHERE user_active = 1 AND user_id NOT IN (2','3)
That's obviously invalid SQL syntax.
You either need to drop the ' in the implode (although this will cause a SQL error if $ilch_to_phpbb is empty, because NOT IN () is invalid SQL), or add ' around it in the query:
AND user_id NOT IN ('".implode("','", $ilch_to_phpbb)."')

Related

PHP syntax error in SELECT mySQL query

I am trying to execute this mySQL query in PHP.
$sql = "SELECT * FROM Property
WHERE CONCAT(name, '',
contact_number , '',
hostel_address,'',
renter_name,'',
other_details,'',
date_posted,'') LIKE '%".$var."'
ORDER BY STR_TO_DATE(date_posted,'%d/%m/%Y')";
An I am getting following error:
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 'ORDER BY STR_TO_DATE(date_posted,'%d/%m/%Y') ASC' at line 1
Any help here :(
may try this:
$sql = "SELECT * FROM Property WHERE CONCAT(name, '',contact_number , '', hostel_address,'',renter_name,'',other_details,'',date_posted,'') LIKE '%".$var."' ORDER BY STR_TO_DATE(date_posted,'%d/%m/%Y')";
Note that there should be an ending single quote before ORDER BY

Laravel Concat - Syntax or Access Violation

Trying to write an MySQL Query to search on users based on two columns :
first_name
surname
I'd like to Concatenate first_name and surname as name and then write a search query based on that.
I've currently written some PHP code, But I keep getting the 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 '%?%' at line
1 (SQL: select users.* from users where CONCAT(first_name, ' ',
surname) LIKE %Stu%)
My current PHP code is as follows :
// Search Based on Q..
$q = $request->input('q');
$results = User::select('users.*')->WhereRaw("CONCAT(`first_name`, ' ', `surname`) LIKE %?%", [$q])->get();
I suspect I'm just getting the syntax wrong, But any help would be appreciated.
Thank You!
Move % to parameter:
$results = User::select('users.*')
->WhereRaw("CONCAT(`first_name`, ' ', `surname`) LIKE ?", ['%' . $q . '%'])
->get();
two errors in your query first as fullname not mention and second like operator.. i have fix these errors
$q = $request->input('q');
$results = User::select('users.*')
->WhereRaw("CONCAT(`first_name`, ' ', `surname`) as fullname
LIKE ?", ['%' . $q . '%'])->get();

I cannot figure out why I am getting this MySQL syntax error (PHP PDO) (MySQL)

I am trying to do a simple insert into my MySQL database, but I get this syntax error:
Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access
violation: 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
'Combined Authority - ICT Service Desk Technician (WYCA 53) ' at line 2
Why?
Query:
$conn->exec("INSERT INTO jobs (jobname, category, contract, link)
SELECT * FROM (" . $name[$i] . "," . $category[$i] . "," . $contract[$i]
. "," . $link[$i] . ") AS tmp
WHERE NOT EXISTS (
SELECT link FROM jobs WHERE link = '" . " " . $link[$i] . ") LIMIT 1;");
$sql printed:
INSERT INTO jobs (jobname, category, contract, link) SELECT * FROM ( West
Yorkshire Combined Authority - ICT Service Desk Technician (WYCA 53)
Details ,' Other ',' Other
','https://bradford.engageats.co.uk/ViewVacancyV2.aspx?
enc=mEgrBL4XQK0+ld8aNkwYmF3VpPuSfX9mpz94c96U/BBgu1IZbwnQ0d+smFL6YrlPhdWkSGi559WmVou+xCXKsYHbHKP0EyHRCwf+vYTu8aYRJbtJgz78Wm2KQgu+LktushGT2Rg0PHjiRMA2Xyn4gw==') AS tmp WHERE NOT EXISTS ( SELECT link FROM jobs WHERE link ='https://bradford.engageats.co.uk/ViewVacancyV2.aspx?enc=mEgrBL4XQK0+ld8aNkwYmF3VpPuSfX9mpz94c96U/BBgu1IZbwnQ0d+smFL6YrlPhdWkSGi559WmVou+xCXKsYHbHKP0EyHRCwf+vYTu8aYRJbtJgz78Wm2KQgu+LktushGT2Rg0PHjiRMA2Xyn4gw==') LIMIT 1;
Apologies for the poor formatting above. Please copy and paste it into a text editor to view it better.
EDIT:
Strangely, this query works with dummy values, but it's still not working for arrays
INSERT INTO jobs (jobname, category, contract, link)
SELECT * FROM (SELECT 'Test', 'Test2',
'Test3','https://bradford.engageats.co.uk/ViewVacancyV2.aspx?
enc=mEgrBL4XQK0+ld8aNkwYmEUlxXraCLcDtY5P6rS92ks+pMDnlWa9QO6M/Df/HLticzgbgVWV
YayJj+zNDXalJnejkDY/4/gH0pIF9KyvMFXjn0u0quGSUzf4M/Gh0wF0MqIRgwLERFf+xXj6lw4s
tQ==') AS tmp
WHERE NOT EXISTS (
SELECT link FROM jobs WHERE link = 'https://bradford.engageats.co.uk/ViewVacancyV2.aspx?enc=mEgrBL4XQK0+ld8aNkwYmEUlxXraCLcDtY5P6rS92ks+pMDnlWa9QO6M/Df/HLticzgbgVWVYayJj+zNDXalJnejkDY/4/gH0pIF9KyvMFXjn0u0quGSUzf4M/Gh0wF0MqIRgwLERFf+xXj6lw4stQ=='
) LIMIT 1;
try this select query and do same with other variables :
SELECT * FROM ("'".$name[$i]."','".$category[$i]."', '".$contract[$i] ."', '". $link[$i] ."'") AS tmp
I can spot three different problems:
First of all, you are inventing your own SQL syntax and the server is not amused. You cannot SELECT * FROM (anything you want). You can only select from tables, views or subqueries.
Secondly, when you type e.g. foo in SQL the database engine needs a way to figure out if you mean a table or column or you mean literal word. The method used is single quotes:
SELECT foo AS this_is_a_column, 'foo' AS this_is_a_value
FROM bar
You can find more details at What is the difference between an identifier and a literal?
Last but not least, your overall use of the PDO extension is wrong. PDO provides a way to separate code and data but you are not using it. Rather than this:
$conn->exec("SELECT link FROM jobs WHERE link = '" . " " . $link[$i] . ") LIMIT 1;");
... you should be doing something like this:
$stmt = $conn->prepared("SELECT link FROM jobs WHERE link=? LIMIT 1");
$stmt->execute($stmt, array($link[$i]));
Use Quotes for string litterals
backticks for columns and tables names.
For more reference Check: http://php.net/manual/en/pdo.errorinfo.php

Use array in where clause not working

Hi I have a query in php file which is used to filter the data in file from mysql database
$_SESSION['sc_session'][$this->Ini->sc_page]['grid_deposit']['where_orig'] = " where Reg_no = \"69\"";
In this line if Reg_no = \"69\"" , if i change the 69 to any value data is being modified but if i use an array instead of 69 then its not working like this
$_SESSION['sc_session'][$this->Ini->sc_page]['grid_deposit']['where_orig'] = " where Reg_no = " . $fc . "";
But if i use
$fc = 69;
echo $fc;
Then its working but not on that line please tell me how to code this The error on which i get is
Error
Error while accessing the database:
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
select count(*) from deposit where Reg_no =
from your Reg_no =\"69\""
and your Reg_no =". $fc."";
are you not missing the "" of the $fc
$_SESSION['sc_session'][$this->Ini->sc_page]['grid_deposit']['where_orig'] = " where Reg_no = \"" . $fc . "\"";
to match your 69 example.
In your original question you stated this error text
Error
Error while accessing the database:
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
select count(*) from deposit where Reg_no =
If the $fc would be an array you would see this in the query as such. If i remember correctly it would look like that ...
Error
Error while accessing the database:
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
select count(*) from deposit where Reg_no = Array
As it does not i assume that the variable $fc is empty. Did you check the variable or better create the query and log it somewhere to check the query as it gets sent to the sql server.
As mentioned, if it would be an array PHP would convert it when wrongly used to the text "Array" which you should find in the query.
i guess you try to do something like
"where Reg_no IN (".implode(",", $fc).")";

Fatal problem within my MySql queries

I'm having a problem regarding these 3 queries:
select count(*) as rec_count from test_history inner join
test_detail on test_history.history_id = test_detail.test_history_id
where test_history.history_id in ({$_SESSION['history_ids']})
and test_history.user_id = $userID
and
select count(*) as correct_answers from test_history inner join test_detail
on test_history.history_id = test_detail.test_history_id
where test_history.history_id in ({$_SESSION['history_ids']}) and
test_history.user_id = $userID and is_correct = 1
and this one
select * from test_topics inner join
test_topics_link on test_topics.`topic_id` = test_topics_link.`topic_id`
where test_topics_link.test_id in ({$_SESSION['ids_tests']}) and percentage > 0
I'm always 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 ') and
test_history.user_id = 82' at line 3
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 ') and
test_history.user_id = 82 and is_correct = 1' at line 2
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
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 ') and percentage > 0' at
line 3
Since $_SESSION['ids_tests'], I suppose, is an array, you should write implode(',',$_SESSION['ids_test']), e.g. in PHP it should be:
$query = "select * from test_topics inner join
test_topics_link on test_topics.`topic_id` = test_topics_link.`topic_id`
where test_topics_link.test_id in (".
implode(',',$_SESSION['ids_tests']).") and percentage > 0";

Categories