MySQL single SELECT showing error in PHP but not in PHPMYADMIN - php

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

Related

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.

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

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 error Reatest-n-per-group

Help me!
SELECT ma_forum.*, ma_forum_cat.*
FROM ma_forum, ma_forum_cat
JOIN ma_forum_comentarios ON ma_forum_comentarios.not_id = ma_forum.not_id
GROUP BY ma_forum.not_id
WHERE ma_forum.notcat_id=ma_forum_cat.notcat_id AND ma_forum.notcat_id='".$notcat_id."'
AND ma_forum.not_status='Ativo'
ORDER BY MAX(ma_forum_comentarios.comnot_data) DESC
"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 ma_forum.notcat_id=ma_forum_cat.notcat_id AND ma_forum.notcat_id='1' ' at line 9"
Your query is in the wrong order; GROUP BY should be after WHERE and before ORDER BY:
SELECT ma_forum.*, ma_forum_cat.*
FROM ma_forum, ma_forum_cat
JOIN ma_forum_comentarios ON ma_forum_comentarios.not_id = ma_forum.not_id
WHERE ma_forum.notcat_id=ma_forum_cat.notcat_id AND ma_forum.notcat_id='".$notcat_id."'
AND ma_forum.not_status='Ativo'
GROUP BY ma_forum.not_id
ORDER BY MAX(ma_forum_comentarios.comnot_data) DESC
I don't know what database you're using, but here's a link to MySQL's SELECT syntax

Mysql query for using count on a view in php

I have a query:
$result = mysql_query("CREATE VIEW temporary(IngList) AS (
SELECT DISTINCT (r1.Ingredient)
FROM recipes r1,
recipes r2
WHERE r1.Country = '$temp'
AND r2.Country = '$temp2'
AND r1.Ingredient = r2.Ingredient)
SELECT COUNT(*) FROM temporary");
I want the query to make a view called temporary and have it return a count of the number of rows in the view temporary. I know this code works without the SELECT COUNT(*) because I checked my database and the view is created.
Yet this code throws the 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 'SELECT COUNT(*) FROM temporary' at line 1
I checked the syntax and it seems to be correct. What seems to be the problem because its quite frustrating.
From the mysql_query documentation:
mysql_query() sends a unique query (multiple queries are not supported)...
You can't create the view, and select from it in a single mysql_query. The view is unnecessary:
$sql = sprintf("SELECT COUNT(DISTINCT r1.Ingredient)
FROM recipes r1
WHERE r.country = '%s'
AND EXISTS(SELECT NULL
FROM recipes r2
WHERE r2.Country = '%s'
AND r1.Ingredient = r2.Ingredient)",
$temp, $temp2);
$result = mysql_query($sql);
For starters you have two statements. What you're writing looks more like a stored procedure. Even if it worked, you would need a semicolon at the end of the first statement. And another statement somewhere saying "DROP VIEW ...." when you are done.
And a temp view is a bit of a non sequitur. I can't find any reference to "CREATE VIEW temporary". Or maybe it's to create a view named temporary with an argument? Views don't take arguments.
I think you might get what you want with a semi-simple SQL statement something like:
$result = mysql_query(
"SELECT COUNT(DISTINCT r1.Ingredient)
FROM recipes r1
JOIN recipes r2 ON r1.Ingredient = r2.Ingredient
WHERE r1.Country = '$temp'
AND r2.Country = '$temp2'");

Categories