Why is this a SQL syntax error? - mysql

I have this query in Perl:
my $pkg="%";
my $sql = "SELECT pid, CAST(pid as UNSIGNED) AS l FROM xmld ORDER BY l WHERE pkg LIKE ?";
my $files_ref = $dbh->selectcol_arrayref($sql, undef, $pkg);
It crashes with:
DBD::mysql::db selectcol_arrayref 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 'WHERE pkg LIKE '%'' at line 1 at ...
I have been looking at this statement for hours, tried various things, but no luck.
Where is that extra single quote coming from and how do I get rid of it?

order by goes after the where:
SELECT pid, CAST(pid as UNSIGNED) AS l
FROM xmld
WHERE pkg LIKE ?
ORDER BY l;

Related

Trying to perform divison using MySQL subqueries but getting syntax error

I am trying to get the value from MySQL subqueries but I am getting 'Error Code: 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 1'
The code is:
UPDATE national_avg_tariff SET Value= (CASE WHEN ((SELECT * FROM((SELECT Value
/ ((Select Sum_CPP_MlnRs from ppp_fy2020_2030_annex4.FY_CPP_exclKE_MlnRs where FiscalYear=2020) + (Select UoSC_MoF_exclKE_MlnRs from ppp_fy2020_2030_annex4.FY_UoSC_MoF_exclKE_MlnRs where FiscalYear=2020))/ (SELECT Sum(Sum_of_AvgMW) FROM ppp_fy2020_2030_annex4.AVG_OF_FY_MAX_DEMAND where FiscalYear=2020 GROUP BY FiscalYear)FROM tariff_true_cost.national_avg_tariff WHERE Sector='Residential' AND Category='Up to 50 Units' AND Parameter= 'Fixed Charge(Rs/kW/M)' AND FiscalYear=2020)temptbl8))>0)
THEN
(SELECT * FROM((SELECT Value
/ ((Select Sum_CPP_MlnRs from ppp_fy2020_2030_annex4.FY_CPP_exclKE_MlnRs where FiscalYear=2020) + (Select UoSC_MoF_exclKE_MlnRs from ppp_fy2020_2030_annex4.FY_UoSC_MoF_exclKE_MlnRs where FiscalYear=2020))/ (SELECT Sum(Sum_of_AvgMW) FROM ppp_fy2020_2030_annex4.AVG_OF_FY_MAX_DEMAND where FiscalYear=2020 GROUP BY FiscalYear)FROM tariff_true_cost.national_avg_tariff WHERE Sector='Residential' AND Category='Up to 50 Units' AND Parameter= 'Fixed Charge(Rs/kW/M)' AND FiscalYear=2020)temptbl8))
ELSE NULL END) where FiscalYear=2020 AND Parameter='Fixed Charge(%)';
The code doesn't show any red underline or pre-execution error something, the error only appears once I execute it.
Any help in rectifying syntax error of this piece of code would be appreciable. Thanks

Error Number: 1064 when select from database

when select images from a database below error is shown & after i delete record in the database its remain the same number inside (id)
this is the error
Error Number: 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 '16,19,1,2,1,2,1 )' at line 1
select * from images where id in(,16,19,1,2,1,2,1 )
Filename: controllers/Admin.php
Line Number: 1046
and this is select statment :
if (!empty($data->images)) {
$qry = $this->db->query("select * from images where id in($data-
>images )");
$res['results'] = $qry->result();
}
and now my database is empty and this error still remaining ?
just remove the leading comma. like this.
select * from images where id in( 16,19,1,2,1,2,1 )
there is a leading comma in $data->images

MySQL - PDO bindParam

$request_id_col and $request_id are strings. However, the request_id_col type in the table is an integer.
$stmt = $db->prepare(' SELECT r.qty, d.name
FROM requested_devices r
JOIN devices d ON r.device_id = d.id
WHERE r.:request_id_col = :request_id
ORDER BY r.id');
$stmt->bindParam(':request_id_col', $request_id_col);
$stmt->bindParam(':request_id', $request_id);
$stmt->execute();
I'm receiving 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 ''sample_id' = '101' ORDER BY r.id' at line 4'
How do I make a query by correctly using bindParam's?
You can't bind table or column names. Only values.

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

This is query I am using to insert into the table.
I am getting the error like this
$sql = mysql_query("INSERT INTO userinfo(runnername,runnerdob,runnerage,runnergender,runneraddress,runnercity,runnerstate,runnerpincode,runneremail,runnerpassword,runnermobilenumber,e_name,e_relationship,e_address,e_mobilenumber,height,weight,bloodgroup,category,tshirtsize,accountcreationdate,lastlogin,timestamp,registereduser,ipaddress,status)
VALUES('$runnername','$dt',$runnerage,'$runnergender','$runneraddress','$runnercity','$runnerstate','$runnerpincode','$runneremail','$epassword','$runnermobilenumber','$e_name','$e_relationship','$e_address','$e_mobilenumber','$height','$weight','$bloodgroup','$category',$tshirtsize,'$accountdate','Y','$t','Y','$ip',1") or die(mysql_error());
This is generated output
INSERT INTO
userinfo(runnername,runnerdob,runnerage,runnergender,runneraddress,runnercity,runnerstate,runnerpincode,runneremail,runnerpassword,runnermobilenumber,e_name,e_relationship,e_address,e_mobilenumber,height,weight,bloodgroup,category,tshirtsize,accountcreationdate,lastlogin,timestamp,registereduser,ipaddress,status)
VALUES('VIDHYA PRAKASH R','1985-04-08',29,'M','12 DIVINE
RESIDENNCY','coimbatore','TAMILNADU','641035','vidhyaprakash85#gmail.com','FU4A31/GhcmRItAHb97lNtrjRZr+y1yG4arxawG/qEs=','9944524864','rajendran','father','12
DIVINE RESIDENNCY coimbatore TAMILNADU
641035','9894773083','6','6','A1+ve','M',42,'11-07-2014
12:11:02','Y','1405060862','Y','127.0.0.1',1)
but I am getting error as
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
I am breaking my head for past two hours.
Any one please help me ?
The issue was at the end of the query, writing long insert queries on single line should be avoided and better to write in multiple lines which helps to find the issue almost immediately , or the best is to use prepared statement with PDO or mysqli.
Your query ends as 'Y','$ip',1") which was missing )
Here how the query should be
$sql = mysql_query(
"INSERT INTO userinfo
(
runnername,
runnerdob,
runnerage,
runnergender,
runneraddress,
runnercity,
runnerstate,
runnerpincode,
runneremail,
runnerpassword,
runnermobilenumber,
e_name,
e_relationship,
e_address,
e_mobilenumber,
height,
weight,
bloodgroup,
category,
tshirtsize,
accountcreationdate,
lastlogin,
timestamp,
registereduser,
ipaddress,
status
)
VALUES
(
'$runnername',
'$dt',
$runnerage,
'$runnergender',
'$runneraddress',
'$runnercity',
'$runnerstate',
'$runnerpincode',
'$runneremail',
'$epassword',
'$runnermobilenumber',
'$e_name',
'$e_relationship',
'$e_address',
'$e_mobilenumber',
'$height',
'$weight',
'$bloodgroup',
'$category',
$tshirtsize,
'$accountdate',
'Y',
'$t',
'Y',
'$ip',
1
)"
) or die(mysql_error());
Are you missing a close bracket at the end of your values statement?
Instead of
'$ip',1")
Shouldn't it be
'$ip',1)")

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL

#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 '-sporocilo us, uporabnik u WHERE ( 's%' LIKE
u.uuser) AND (s.SID = us.SID) AND (' at line 2
this is the error i get ...
SQL:
SELECT us.ussender, s.ssubject, s.scontent, us.ustimesend, us.usstatus,
FROM sporocilo s, uporabnik-sporocilo us , uporabnik u
WHERE ($user LIKE u.uuser) AND (s.SID = us.SID) AND (us.usreciever LIKE u.uuser),
ORDER BY us.ustimesend ASC;
Database: http://my.jetscreenshot.com/20224/20140415-ls6n-41kb
If your column identifiers are going to contain dashes you must wrap them in ticks. Otherwise MySQL assumes you are performing a subtraction operation.
SELECT us.ussender, s.ssubject, s.scontent, us.ustimesend, us.usstatus,
FROM sporocilo s, `uporabnik-sporocilo` us , uporabnik u
WHERE ($user LIKE u.uuser) AND (s.SID = us.SID) AND (us.usreciever LIKE u.uuser),
ORDER BY us.ustimesend ASC;
The syntax error is missing quotes, here:
WHERE ($user LIKE u.uuser) AND (s.SID = us.SID) AND (us.usreciever LIKE u.uuser),
plus the comma at the end of that line.
There are other problems with your query, but that is beyond the scope of your question.