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

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

Related

Mysqli query with zerofill

Hello stackoverflow community, I need help with this mysqli query. My auto increase column in database is ZERO-FILL element for example 00001 so when I try to query like this:
$stmt = $mysqli->query("UPDATE ".$db_table_prefix."korteles
SET
vardas = '".$this->clean_k_name."',
pavard = ".$this->clean_k_surname.",
WHERE korteles_nr = '00001'");
And I get 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 'WHERE korteles_nr = 00001' at line 18
I was searching Internet for solving this problem. But can't find so please help!
Query should be like this:-
$stmt = $mysqli->query("UPDATE ".$db_table_prefix."korteles
SET
vardas = '$this->clean_k_name',
pavard = '$this->clean_k_surname'
WHERE
korteles_nr = '00001'");
You have to remove the comma after $this->clean_k_surname variable

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

CodeIgniter - Error Number: 1146 (Table doesn't exist after execute a "CREATE TABLE" command)

I've posted yesterday an issue with an mySQL update syntax on CodeIgniter in here:
CodeIgniter - MySQL Error 1064 (Update table1 inner join table2(...))
But now after I solved that problem, another one come up. Now the update query doesn't know the new created table. But if I change to a select statement, it works smoothly.
For that reason I've decided to post the full script.
Code:
<?php
$this->load->database();
$query_tbaux='CREATE TABLE IF NOT EXISTS STUDY_LIST_AUX AS (
SELECT DISTINCT p.pat_id, p.pat_custom1 age, p.pat_name,
p.pat_sex, s.study_iuid, p.pat_birthdate, s.accession_no,
s.study_datetime date_s, s.study_desc, s.mods_in_study, s.pk,
c.institution, s.study_block, s.study_urgent,
\'0000-00-00 00:00:00\' AS \'report_date\', \'{null}\' AS \'report_status\',
s.study_tipo,
s.study_src,
s.study_consulta
FROM study s
INNER JOIN patient p ON s.patient_fk = p.pk
INNER JOIN series c ON c.study_fk = s.pk
INNER JOIN rel_users_hosp u ON u.hosp_id = c.institution
WHERE s.study_datetime >= \'2015-04-26 00:00:00\'
AND s.study_datetime <= \'2015-04-30 23:59:59\'
AND s.study_iuid IS NOT NULL
AND u.user_id = \'admin\'
)';
if ($this->db->query($query_tbaux))
{
echo "Q True!<br><br>";
$data = array(
'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',
);
$this->db->update('STUDY_LIST_AUX, study_report', $data, array('STUDY_LIST_AUX.study_iuid'=>'study_report.study_iuid'));
}
else
{
echo "Q False<br><br>";
};
?>
Display/Error:
Q True!
A Database Error Occurred
Error Number: 1146
Table 'pacsdb.STUDY_LIST_AUX,' doesn't exist
UPDATE STUDY_LIST_AUX, study_report 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' WHERE
STUDY_LIST_AUX.study_iuid = 'study_report.study_iuid'
I've checked phpmyadmin after refresh the page and the table really exists and it contains the data from the select statement.Can you please tell me what mistake I did?
This may cause you real problem.Remove ,study_report
after your table name.Try it
$this->db-
>update('STUDY_LIST_AUX',
$data,
array('STUDY_LIST_AUX.study_iuid'=>
'study_report.study_iuid'));
Pay closer attention.
Table 'pacsdb.STUDY_LIST_AUX,'
See that comma at the end? That should not be there. The error message is right.
The solution was more simple than it looks.
$query_update = "UPDATE STUDY_LIST_AUX
INNER JOIN study_report
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
WHERE STUDY_LIST_AUX.study_iuid = study_report.study_iuid";
$this->db->query($query_update);
It seems to be a issue with the CodeIgnigter Framework on the update integrated function ($this->db->update(...)). It doesn't work with sub queries, and the best solution goes for using the normal query execution function ($this->db->update($query))
Anyway, thanks for the help ;) All your answers were very helpful in a way to get to this solution.

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

MySql Syntax error - 42000

I'm getting this error :
Warning: PDO::query() [pdo.query]: 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 'When, RDV, Comments FROM distributions WHERE IDFond = 1' at line 1 in /Applications/XAMPP/xamppfiles/htdocs/JG/DistributionManager.class.php on line 56
When executing this code :
$Distribution_Manager->getListByFunds($Selected_Fond->id());
foreach ($Distribution_Manager as $Distrib)
{
echo $Distrib->Comments();
}
Here is the concerned function :
public function getListByFunds($FundID)
{
$Distribution = array();
$q = $this->_db->query('SELECT id, IDClient, IDFond, Who, When, RDV, Comments FROM distributions WHERE IDFond = '.$FundID);
while ($donnees = $q->fetch(PDO::FETCH_ASSOC))
{
$Distribution[] = new Distribution($donnees);
}
return $Distribution;
}
Should be a little mistake but I'm lagging on it for almost 50 minutes !
Thanks in advance for the help ;)
WHEN is a mysql reserved word, so try using a different column name or enclose WHEN in backquotes.
When is mysql keyword, try this
SELECT id, IDClient, IDFond, Who, When AS anything..
or enclose this keyword to backquotes
SELECT id, IDClient, IDFond, Who, `When`, RDV..
Give back quotes for column names like id and try it
You're never closing your quotes on your statement.

Categories