Error Number: 1064 - php

I'm getting this error while joining two tables in codeigniter.
Error Number: 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 '.`id = a`.`assignee_key where a`.`assigned_to="office" and
user`.`id="53"`' at line 1
select a`.* from address as `a LEFT JOIN user ON user`.`id = a`.`assignee_key where a`.`assigned_to="office" and user`.`id="53"
$this->db->select('select a.* from address as a LEFT JOIN user ON user.id = a.assignee_key where a.assigned_to="office" and user.id="'.$user_id.'"');
$query = $this->db->get();
return $query->row();

You have got two select. Here is the better/structured version of your query.
$this->db->select("a.*")
->from("address as a")
->join("user","user.id = a.assignee_key","left")
->where(array("a.assigned_to"=>"office","user.id"=>$user_id));
$query = $this->db->get();
return $query->row();
UPDATE: For the comment try left outer join
->join("user","user.id = a.assignee_key","left outer")
It preserves the unmatched rows from the first (left) table, joining them with a NULL row in the shape of the second (right) table.

You have problem in alias table name. Try this
$this->db->select('select a.* from address a LEFT JOIN user u ON u.id = a.assignee_key where a.assigned_to="office" and u.id="'.$user_id.'"');
$query = $this->db->get();
return $query->row();

Related

MySQL single SELECT showing error in PHP but not in PHPMYADMIN

There are some questions on stackoverflow about it but only with insert, what about that? :
Sql in phpmyadmin:
SELECT logins.user as user,
logins.id as usid,
rozliczenia.godziny as godziny,
rozliczenia.stawka as stawka,
rozliczenia.premia as premia,
rozliczenia.premiainna as premiainna
FROM logins
LEFT JOIN rozliczenia
ON logins.id=rozliczenia.userid
AND DATE(rozliczenia.data) BETWEEN DATE('2015-01-01') AND DATE('2015-01-31')
WHERE logins.user NOT IN ('SUPERUSER', 'agata', 'tomek')
GROUP BY logins.user
ORDER BY logins.id
It works, but in php:
$sql = "SELECT logins.user as user, logins.id as usid, rozliczenia.godziny as godziny, rozliczenia.stawka as stawka, rozliczenia.premia as premia, rozliczenia.premiainna as premiainna FROM logins LEFT JOIN rozliczenia ON logins.id=rozliczenia.userid AND DATE(rozliczenia.data) BETWEEN DATE('2015-10-01') AND DATE('2015-10-31') WHERE logins.user NOT IN ('SUPERUSER', 'agata', 'tomek') GROUP BY logins.user ORDER BY logins.id;";
//The same sql
if(!$result = $polaczenie->query($sql)){
return FALSE;
}
while ($rowV = $result->fetch_array())
{
...
It kinda works, but returns:
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 '2015-10-01') AND DATE('2015-10-31') WHERE logins.user NOT IN ('SUPERUSER', '' at line 1
What's wrong?
Know all of a sudden more of my scripts return that error. I'm tired of this
EDIT
When I delete
WHERE logins.user NOT IN ('SUPERUSER', 'agata', 'tomek')
GROUP BY logins.user
ORDER BY logins.id
it works, but I need it. If I delete only where or only grouop, it doesn't change anything
Maybe i'm wrong but, your first AND should be after your WHERE.
It was weird, but when i rewrited my code, it worked

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 ...";

MySQl Error #1064 Syntax error

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.

selecting where in php mysql if where is equals to array index

hey guys im trying to query something in php but the WHERE is a index from array this is what i did
$data=array();
while ($row = mysql_fetch_array($result))
{
$item = array(
'sec_id'=>$row['section_id'],
'sec_name'=>$row['section_name'],
'sec_dept'=>$row['section_department'],
'sec_lvl'=>$row['section_level'],
'advisory_id'=>$row['advisory_id'],
'first_name'=>$row['f_firstname'],
'last_name'=>$row['f_lastname'],
'middle_name'=>$row['f_middlename'],
'advisor_id'=>$row['faculty_id'],
);
$get_subjects = "SELECT subject_name
FROM subjects
WHERE level = '".$row['section_level']."' ";
$result_get_subjects =mysql_query($get_subjects)or die(mysql_error());
$subjects_count = mysql_num_rows($result_get_subjects);
$check_archive_subjects = " SELECT b.subject_name
FROM registrar_grade_archive a
LEFT JOIN subjects b ON(a.subject_id=b.subject_id)
LEFT JOIN section c ON(a.section_id = c.section_id)
WHERE a.advisor_faculty_id ='".$row['faculty_id']."'
WHERE a.section_id ='".$row['section_id']."'
GROUP BY b.subject_name ASC
" ;
$query_checking =mysql_query($check_archive_subjects)or die(mysql_error());
$subjects_count_sent = mysql_num_rows($query_checking);
but unfortunately i got an error in $check_archive_subjects that says:
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 a.section_id ='24'
is there any other way to put an array index in where clause in mysql. I know mysql is deprecated and ill be switching to mysqli after i finished this project so pardon me guys. thanks in advance
Multiple WHERE conditions should be joined using boolean keywords AND or OR. You don't issue multiple WHERE clauses.
Also, please read this regarding the MySQL extension - https://stackoverflow.com/a/12860046/283366
Try the below query
SELECT b.subject_name
FROM registrar_grade_archive a
LEFT JOIN subjects b ON(a.subject_id=b.subject_id)
LEFT JOIN section c ON(a.section_id = c.section_id)
WHERE a.advisor_faculty_id ='".$row['faculty_id']."' AND a.section_id ='".$row['section_id']."'
GROUP BY b.subject_name ASC
Explanation
Using multiple WHERE clause like above is not accepted in MySQL. Try replacing the 2nd WHERE using an AND or using an OR depending on your requirement. I have used AND

Categories