MySQl Error #1064 Syntax error - php

I get 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 '' at line 4 SQL=SELECT company.contactname AS name, company.contactemail AS email, job.title, job.sendemail FROM `kecobo_js_job_companies` AS company JOIN `kecobo_js_job_jobs` AS job ON job.companyid = company.id WHERE job.id =
with this query:
$jobquery = "SELECT company.contactname AS name, company.contactemail AS email, job.title, job.sendemail
FROM `#__js_job_companies` AS company
JOIN `#__js_job_jobs` AS job ON job.companyid = company.id
WHERE job.id = ".$jobid;
Does anybody has a suggestion what could be wrong?

$jobquery = "SELECT company.contactname AS name, company.contactemail AS email, job.title, job.sendemail
FROM `#__js_job_companies` AS company
JOIN `#__js_job_jobs` AS job ON job.companyid = company.id
WHERE job.id = '".$jobid."'";
Consider injection issuses

Consider using MySQLi or PDO. As for your question $jobid is empty you can see it in you query ending in equal sign.

Related

right syntax to use near '%+ads.title+%\r\n AND\r\n products_request

I would like to compare two columns by like :
$sql = "SELECT
products_requests.id as req_id,ads.id as send_id,
products_requests.user_id,ads.des,ads.title
FROM
products_requests,ads
WHERE
products_requests.title LIKE '%'+ads.title+'%'
AND
products_requests.ads_cat_id = ads.ads_cat_id
AND
products_requests.ads_sub_cat_id = ads.ads_sub_cat_id
AND
products_requests.price_type_id = ads.price_type_id
AND
products_requests.province_id = ads.province_id
AND
products_requests.city_id = ads.city_id";
but I get 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 '%+ads.title+%\r\n AND\r\n products_request' at line 9"
In MySQL, perform string concatenation operations with the CONCAT function
... LIKE CONCAT('%',ads.title,'%')
I also recommend you ditch the old-school comma syntax for the join operation, and use the JOIN keyword instead. And move the join predicates into an ON clause. And consider using shorter table aliases to qualify the column references.
SELECT req.id AS req_id
, ads.id AS send_id
, req.user_id
, ads.des
, ads.title
FROM products_requests req
JOIN ads
ON req.title LIKE CONCAT('%',ads.title,'%')
AND req.ads_cat_id = ads.ads_cat_id
AND req.ads_sub_cat_id = ads.ads_sub_cat_id
AND req.price_type_id = ads.price_type_id
AND req.province_id = ads.province_id
AND req.city_id = ads.city_id

CodeIgniter - MySQL Error 1064 (Update table1 inner join table2(...))

Is now 6am in the morning and i'm still struggling to execute a query with the CodeIgniter PHP framewok. Hope you guys can help me
Code:
$query='
UPDATE `STUDY_LIST_AUX`
INNER JOIN `study_report`
ON `STUDY_LIST_AUX.study_iuid`=`study_report.study_iuid`
SET `STUDY_LIST_AUX.report_date`=DATE_FORMAT(`study_report.report_date`,\'%Y-%m-%d %h:%i:%s\'), `STUDY_LIST_AUX.report_status` = `study_report.report_status`
';
if ($this->db->query($query))
{
echo "True!<br><br>";
}
else
{
echo "False<br><br>";
};
Error:
A Database Error Occurred
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 'UPDATE STUDY_LIST_AUX INNER JOIN study_report ON `STUDY_LIST_AUX.study_iu' at line 22
I've tried everything, backticks, normal ticks, quote marks, but the error persists. On phpmyadmin the query run successfully.
Any suggestions or ideas will be much appreciated
Thanks in advance guys :)
You can update your query code using this below Active Record code
$data = array('s.report_date' => 'DATE_FORMAT(`study_report.report_date`,\'%Y-%m-%d %h:%i:%s\')','s.report_status' => 'sr.report_status');
$this->db->update('STUDY_LIST_AUX s JOIN study_report sr on s.study_iuid = sr.study_iuid',$data);
This'll help you...
try this. use double quote so you don't have to worry the quote inside
$query="UPDATE STUDY_LIST_AUX
INNER JOIN study_report
ON STUDY_LIST_AUX.study_iuid = study_report.study_iuid
SET STUDY_LIST_AUX.report_date =
DATE_FORMAT(study_report.report_date,'%Y-%m-%d %h:%i:%s'),
STUDY_LIST_AUX.report_status = study_report.report_status";
You have some syntax issue in the query ex:
`STUDY_LIST_AUX.study_iuid`
If you are using backticks then it should be as
`STUDY_LIST_AUX`.`study_iuid`
The correct query should be
$query = "
update `STUDY_LIST_AUX` sla
join `study_report` sr on sr.study_iuid = sla.study_iuid
set
sla.report_date = date_format(sr.report_date,'%Y-%m-%d %h:%i:%s'),
sla.report_status = sr.report_status
";

MySql union Error in codeigniter

I have to fetch values from two tables, I have wrote separate query for fetching from both tables. and both queries worked properly. but I need to get this in one result object. so that I have joined queries using UNION operator. but it throws some error. the query is given below
$query1 = "SELECT dev_members.name,dev_members.id,dev_members.age,dev_members.family_id,dev_family.house_name,dev_ib_account_registration.account_id FROM (dev_members)
JOIN dev_family ON dev_family.id=dev_members.family_id
JOIN dev_ib_account_registration ON dev_ib_account_registration.member_id=dev_members.id
UNION
SELECT dev_members.name,dev_members.id,dev_members.age,dev_members.family_id, dev_family.
house_name,dev_ib_sub_member_registration.account_id FROM (dev_members)
JOIN dev_family ON dev_family.id=dev_members.family_id
JOIN dev_ib_sub_member_registration ON dev_ib_sub_member_registration.member_id=dev_members.id";
$result = $this->db->query($query);
return $result->result();
and the error is:
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 'JOIN dev_family ON dev_family.id=dev_members.family_id JOIN dev_ib_s' at line 8
There is something wired with the usage of brackets in your second query
(dev_members) <---
when i use your query without them in Fiddle query works ,but using with brackets it produces syntax error ,so try them without ()
updated query
SELECT
dev_members.name,
dev_members.id,
dev_members.age,
dev_members.family_id,
dev_family.house_name,
dev_ib_account_registration.account_id
FROM
dev_members
JOIN dev_family
ON dev_family.id = dev_members.family_id
JOIN dev_ib_account_registration
ON dev_ib_account_registration.member_id = dev_members.id
UNION
SELECT
dev_members.name,
dev_members.id,
dev_members.age,
dev_members.family_id,
dev_family.house_name,
dev_ib_sub_member_registration.account_id
FROM
dev_members
JOIN dev_family
ON dev_family.id = dev_members.family_id
JOIN dev_ib_sub_member_registration
ON dev_ib_sub_member_registration.member_id = dev_members.id

Getting values from different tables in mysql query (practical example)

I have two tables: first and second
this and that are primary keys, common and always present on both tables, so I guess there is no need of left joins
$query = "SELECT
first.one,first.going,first.what,first.ever,second.another,second.outre,second.oneplus,second.more,second.anotherthing,second.alldifferent
WHERE second.THIS = first.THAT AND first.is = '1' AND
first.yet = '$variable' AND second.againe = '1'";
The error is the following
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 second.THIS = first.THAT AND first.is = '1' AND first.y' at line 1
But I can't get to understand why this happens.
Any help on this one? Ty very much
You need to specify a table name.
$query = "SELECT first.one, ... ,second.alldifferent WHERE ...";
Should be
$query = "SELECT first.one, ... ,second.alldifferent FROM first, second WHERE ...";

Get Average value of from one table for a specific row in another table php

I am trying to do following.
Select a Hospital with id = hid from table named as hospital.
Add a value "overall_rating" to it and get all the ratings and make avg of it from another table named as hrating
here is my query
$statement = $conn->prepare('SELECT hospital.*,(SELECT AVG(hrating.rating_h) FROM hrating WHERE hid = hospital.hid) as overall_rating WHERE hid=:hid LIMIT 1');
Getting this error
{"error":"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 'WHERE hid='44' LIMIT 1' at line 1"}
Where am i being wrong.
It appears you don't have a " FROM hospital " bit in your query?
SELECT hospital.*,(SELECT AVG(hrating.rating_h)
FROM hrating
WHERE hid = hospital.hid) as overall_rating
FROM hospital -- this line seems to be missing ??
WHERE hid=:hid LIMIT 1
Try this:
SELECT hospital.*, temp.overall_rating
FROM hospital
LEFT JOIN (SELECT hid AVG(rating_h) as overall_rating
FROM hrating group by hid
) temp
on hid = hospital.hid
WHERE hospital.hid=:hid LIMIT 1

Categories