MariaDB / CodeIgniter / PHPColors - php

Can somebody help me with this error.
The PHP below
public function color_sql($filter)
{
$this->db->select('*, sum(count) AS color_sum');
$this->db->from('wallpapers_colors');
$this->db->where('saturation BETWEEN ' . (($filter['saturation'][0]) / 100) . ' AND ' . (($filter['saturation'][1]) / 100));
$this->db->where('lightness BETWEEN ' . (($filter['lightness'][0]) / 100) . ' AND ' . (($filter['lightness'][1]) / 100));
$this->db->where('hue BETWEEN ' . ($filter['hue'][0]) . ' AND ' . ($filter['hue'][1]));
$this->db->group_by('wallpaper_seq_id');
return $this->sql();
}
returns the below error
A Database Error Occurred
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 'BETWEEN0 AND 0.4 AND `lightness` BETWEEN 0.08 AND 0.48 AND `hue` BETWEEN0 AND 20' at line 4
SELECT COUNT(*) AS `numrows` FROM ((SELECT *, sum(count) AS color_sum FROM `WS_wallpapers_colors` WHERE `saturation` BETWEEN0 AND 0.4 AND `lightness` BETWEEN 0.08 AND 0.48 AND `hue` BETWEEN0 AND 20 GROUP BY `wallpaper_seq_id`) wc) JOIN `WS_wallpapers` `w` ON `w`.`wallpaper_seq_id` = `wc`.`wallpaper_seq_id` LEFT JOIN (SELECT `t2`.*, GROUP_CONCAT(c.category_slug order by c1.lvl desc SEPARATOR '/') AS cFullSlug, GROUP_CONCAT(c.category_name order by c1.lvl desc SEPARATOR '|') AS cFullName FROM `WS_closures` `c1` LEFT JOIN `WS_category` `c` ON `c`.`category_seq_id` = `c1`.`ancestor` LEFT JOIN `WS_category` `t2` ON `t2`.`category_seq_id` = `c1`.`descendant` GROUP BY `c1`.`descendant`) as c ON `c`.`category_seq_id` = `w`.`category_seq_id` WHERE `w`.`wallpaper_status` = 1 AND `w`.`wallpaper_approved` = 1
Filename: core/WS_Model.php
Line Number: 76
core/WS_Model.php
public function count() {
$this->eachDay_newCache();
$this->ShutDownCache();
$return = $this->db->count_all_results(); // this is line 76 from WS_Model.php
$this->turnOnCache();
return $return;
}
Anyone have any solution how to solve this error?
(As you can see there is a space in the PHP code after the "BETWEEN" word and I don't know why MariaDB seen without a space.)
Thank you!

Your Error message ( in part ) says
SELECT COUNT(*) AS `numrows` FROM
and in your SQL it says
$this->db->select('*, sum(count) AS color_sum');
They are two different things
So what is the actual code you have at line 76 in core/WS_Model.php because the error code is not coming from the SQL you have shown us.

Related

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

I am getting below mysql database 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 'option, GROUP_CONCAT(DISTINCT year_question_map.year) AS years, GROUP_CONCAT(DIS' at line 1
SELECT `questions`.*, `question_level`.*, `question_answer`.*, GROUP_CONCAT(DISTINCT question_option.question_option SEPARATOR '__') AS option, GROUP_CONCAT(DISTINCT year_question_map.year) AS years, GROUP_CONCAT(DISTINCT exams.exam_name) AS exams FROM `questions` LEFT JOIN `question_option` ON `question_option`.`question_id` = `questions`.`qid` LEFT JOIN `question_level` ON `question_level`.`level_id` = `questions`.`level_id` LEFT JOIN `question_answer` ON `question_answer`.`question_id` = `questions`.`qid` LEFT JOIN `year_question_map` ON `year_question_map`.`question_id` = `questions`.`qid` LEFT JOIN `exams` ON `exams`.`exam_id` = `year_question_map`.`exam_id` WHERE `questions`.`topic_id` = '1' GROUP BY `questions`.`qid` ORDER BY `qid` ASC
I am Using Codeigniter and here is my sql query in my model
$this->db->select("questions.*,question_level.*,question_answer.*,GROUP_CONCAT(DISTINCT question_option.question_option SEPARATOR '__') AS option,GROUP_CONCAT(DISTINCT year_question_map.year) AS years,GROUP_CONCAT(DISTINCT exams.exam_name) AS exams");
$this->db->from('questions');
$this->db->join('question_option','question_option.question_id = questions.qid','left');
$this->db->join('question_level','question_level.level_id = questions.level_id','left');
$this->db->join('question_answer','question_answer.question_id = questions.qid','left');
$this->db->join('year_question_map','year_question_map.question_id = questions.qid','left');
$this->db->join('exams','exams.exam_id = year_question_map.exam_id','left');
$this->db->where('questions.topic_id',$topicID);
$this->db->group_by('questions.qid');
$this->db->order_by('qid','ASC');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result();
} else {
return FALSE;
}
You are using MYSQL Reserve Word in your query:
GROUP_CONCAT(DISTINCT question_option.question_option SEPARATOR '__') AS OPTION,
Note that OPTION is a reserve word you must need to use backtick or change it with other name.
This should be:
GROUP_CONCAT(DISTINCT question_option.question_option SEPARATOR '__') AS `OPTION`,
For reference, you can check the list of Reserve words here:
https://dev.mysql.com/doc/refman/5.5/en/keywords.html
(R) with any word in given below reference indicates this is a reserve word.

How to use REGEXP in MYSQL to match dates IN array?

I am trying to match the year from the publication_date of a table in a lot of years. I am using laravel (PHP).
I have written something like:
if($this->request->has('publication_years')){
$years = implode('-[0-9]{2}-[0-9]{2}|', explode(',', $this->request->get('publication_years'))) . '-[0-9]{2}-[0-9]{2}';
$model = $model->whereExists(function ($query) use ($years)
{
$query->select(DB::raw(1))
->from('patent')
->whereRaw('patent.id = document.documentable_id')
->whereRaw('document.documentable_type = "patent"')
->whereRaw('(patent.publication_date REGEXP (' . $years . '))');
});
}
The years come like ?publication_years=2015,2016. I explode them use the , delimiter and then implode them to make the REGEXP pattern in a way it matches the date. The dump of the years after this is like 2015-[0-9]{2}-[0-9]{2}|2016-[0-9]{2}-[0-9]{2}.
This works at http://regexr.com/ but I am still getting error so I am guessing there some other syntax for MYSQL. The error that I see is:
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 '[0-9]{2}-[0-9]{2}|2016-[0-9]{2}-[0-9]{2})))' at line 1 (SQL: select count(*) as aggregate from `document` where exists (select * from `folder` where `document`.`folder_id` = `folder`.`id` and (exists (select * from `user` inner join `accessible_by_user` on `user`.`id` = `accessible_by_user`.`user_id` where `accessible_by_user`.`accessible_id` = `folder`.`id` and `accessible_by_user`.`accessible_type` = folder and `user_id` = 1) or exists (select * from `role` inner join `accessible_by_role` on `role`.`id` = `accessible_by_role`.`role_id` where `accessible_by_role`.`accessible_id` = `folder`.`id` and `accessible_by_role`.`accessible_type` = folder and `role_id` in (1)))) and exists (select 1 from `patent` where patent.id = document.documentable_id and document.documentable_type = "patent" and (patent.publication_date REGEXP (2015-[0-9]{2}-[0-9]{2}|2016-[0-9]{2}-[0-9]{2}))))
What am I doing wrong? How can I correct this?
Here is a way to prep the regex for using in MySQL:
$this_request_get_publication_years = "2015,2016,2017";
$years = '^(' . implode('|', explode(',',
$this_request_get_publication_years)) . ')-[0-9]{2}-[0-9]{2}';
echo "(patent.publication_date REGEXP '" . $years . "')";
// => (patent.publication_date REGEXP '^(2015|2016|2017)-[0-9]{2}-[0-9]{2}')
The REGEXP '^(2015|2016|2017)-[0-9]{2}-[0-9]{2}' will only fetch the records that:
^ - at the start of the string, have...
(2015|2016|2017) - either of the 3 years (and more are supported, surely)
-[0-9]{2} - a hyphen followed with 2 digits
-[0-9]{2} - ibid.
The latter two subpatterns can be shrunk into (-[0-9]{2}){2}.

Codeigniter sql syntax error?

Because I codeigniter returns a syntax error?
$handler_feedback = $this->CI->db
->select('
feedback.id as feedback_id,
feedback.titulo,
relacion_feedback_usuario_principal.valor
')
->from('feedback')
->join(
'relacion_feedback_usuario_principal',
'
relacion_feedback_usuario_principal.feedback_id = feedback.id AND
relacion_feedback_usuario_principal.usuario_principal_id = 20
',
'left'
)
->get();
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 '' at line 3
SELECT `feedback`.`id` as feedback_id, `feedback`.`titulo`, `relacion_feedback_usuario_principal`.`valor` FROM (`feedback`) LEFT JOIN `relacion_feedback_usuario_principal` ON `relacion_feedback_usuario_principal`.`feedback_id` = `feedback`.`id` AND
Filename: /dir/file.php
Line Number: 289
Mi original query:
SELECT
feedback.id as feedback_id,
feedback.titulo,
relacion_feedback_usuario_principal.valor
FROM feedback
LEFT JOIN relacion_feedback_usuario_principal ON (
relacion_feedback_usuario_principal.feedback_id = feedback.id AND
relacion_feedback_usuario_principal.usuario_principal_id = 20
)
It should work the way you have written it.
Its as simple as,
$this->db->join('B', 'aCol = bCol AND bOtherCol = 0');
$this->db->get('A');
Anyways can you try the other way (join with a where clause),
$this->db->select('t1.id as feedback_id, t1.titulo, t2.valor');
$this->db->from('feedback as t1');
$this->db->join('relacion_feedback_usuario_principal as t2', 't1.id = t2.feedback_id', 'left');
$this->db->where('t2.usuario_principal_id', 20);
$query = $this->db->get();
relacion_feedback_usuario_principal.feedback_id = feedback.id AND
relacion_feedback_usuario_principal.usuario_principal_id = 20
Do you have a space between AND and relaction_feedback_ususario_principal.usuario_principal_id=20?
I have come to conslusion my code is not bad, it's a bug in codeigniter.

Dynamic SQL Pivot in PHP

I have been on this problem for a couple of days without any Success. Any help would be grateful.
I'm using a dynamic Pivot which works when run in SQL, however when I try to run this query in PHP I get the following error.
Warning:mssql_query() [function.mssql-query]: message: Incorrect
syntax near ','. (severity 15) in XXXXX
I've narrowed it down to how I build the variable in SQL. Although this works in SQL, PHP as a problem with it. I've tested it by defining the variable as SELECT #ColName = '[GEN1.1], [GEN2.2]' which works. I've also tried using a for loop to build the array which didn't work.
SELECT #ColName = ISNULL(#ColName + ', ','') + QUOTENAME(GEN)
Can anyone help with this? Thanks.
An example of my data and SQL query is below.
DECLARE #ColName AS NVARCHAR(MAX);
SELECT #ColName = ISNULL(#ColName + ', ','') + QUOTENAME(GEN)
FROM (SELECT DISTINCT GEN FROM TB) AS Gens
SET #DPQ = N'SELECT STAGE, ' + #ColName + '
INTO #TB2
FROM (see below)
PIVOT (SUM(QTY)
FOR GEN IN (' + #ColName + '))
AS PVTTable
ORDER BY STAGE'
EXEC sp_executesql #DPQ
Table
Stage GEN QTY
A GEN1.1 50
A GEN2.2 125
A GEN2.2 75
C GEN1.1 100
C GEN2.2 25
D GEN1.1 199
D GEN2.2 50

You have an error in your SQL syntax. MySQL Selectgiving error with LIKE and

I'm trying to make a MySQL select for use in a mysql_query function, but I keep getting 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 'LEFT JOIN Orgs ON Jobs.CustOrgID = Orgs.ID
LEFT JOIN Persons ON Jobs.CustPer' at line 11
I've tried everything and searched everywhere, but nothing seems to be working. All help is appreciated.
$Qry = "SELECT
Jobs.ID,
'Jobs.Status',
Jobs.JobNum,
'Orgs.Nme',
'Persons.FirstNme',
'Persons.LastNme',
'JobTypes.JobType',
'Jobs.Dsc',
'Jobs.Notes'
FROM Jobs ";
if($column !== null && $text !== null) {
$Qry .= "WHERE " . $column . " LIKE '%" . $text . "%' ";
}
$Qry .= "LEFT JOIN Orgs ON Jobs.CustOrgID = Orgs.ID
LEFT JOIN Persons ON Jobs.CustPersonID = Persons.ID
LEFT JOIN JobTypes ON Jobs.JobTypeID = JobTypes.ID
ORDER BY JobNum";
SOLUTION:
SELECT ...
FROM ...
LEFT JOIN ...
LEFT JOIN ...
WHERE ...
ORDER BY ...
My WHERE was in the wrong place, it should come after the two LEFT JOINS.
You're inserting the WHERE in the wrong place. It must come AFTER the joins:
SELECT ...
FROM ...
LEFT JOIN ...
LEFT JOIN ...
WHERE ...
ORDER BY ...
you should use backticks instead of single quotes , or you dont have to use backticks if they are not reserved keywords or separated strings .
try this :
$Qry = "SELECT
`Jobs`.`ID`,
`Jobs`.`Status`,
`Jobs`.`JobNum`,
`Orgs`.`Nme`,
`Persons`.`FirstNme`,
`Persons`.`LastNme`,
`JobTypes`.`JobType`,
`Jobs`.`Dsc`,
`Jobs`.`Notes`
FROM Jobs ";

Categories