PDO statement error #1064 - mysql

I am trying to move from PDO quote et PDO prepare and execute, my query is like:
$sql = 'SELECT * FROM nav_top1 WHERE id_top = (SELECT top_fr FROM nav_top WHERE top_fr = :rub ORDER BY top1_order ASC';
echo $sql.'<br>';
$query = $connexion->prepare($sql);
$query->bindParam(':rub', $rub, PDO::PARAM_INT);
$query->execute();
$list = $query->fetchAll();
foreach ($list as $rs) {
$top1_fr = $rs['top1_fr'];
echo $top1_fr;
}
but I get an error:
[Sat Mar 07 14:08:05 2015] [error] [client 105.156.126.211]
PHP Warning: PDOStatement::execute(): 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 '' at line 1 in /home/www/Fashion/fashion.php on line 9, referer...
Thanks in advance

missing brace?
top1_order ASC)

$sql = 'SELECT * FROM nav_top1 WHERE id_top = (SELECT top_fr FROM
nav_top WHERE top_fr = :rub ORDER BY top1_order ASC';
Your are missing braces.
$sql = 'SELECT * FROM nav_top1 WHERE id_top = (SELECT top_fr FROM
nav_top WHERE top_fr = :rub ORDER BY top1_order ASC');

Related

issue in mysql query in codeigniter only if I add if condition

In below code, whenever I am adding below code with if conditions, i am getting error
if($this->ion_auth->is_customer())
$this->db->where('company_database.cdb_customer_id',$this->session->userdata('user_id'));
$this->db->select('company.*, cities.name as company_city, states.name as company_state, countries.name as company_country');
$this->db->from('company as company');
$this->db->join(CITIES.' as cities','cities.id = company.company_city_id' ,'left');
$this->db->join(STATES.' as states','states.id = company.company_state_id' ,'left');
$this->db->join(COUNTRIES.' as countries','countries.id = company.company_country_id' ,'left');
$this->db->join(COMPANY_DATABASE.' as company_database','company_database.cdb_company_id = company.company_id' ,'left');
if($this->ion_auth->is_customer())
$this->db->where('company_database.cdb_customer_id',$this->session->userdata('user_id'));
$this->db->where('company.company_delete_status',NOT_DELETED);
$query = $this->db->get();
echo '<pre>';
echo $this->db->get_compiled_query();
print_r($query->result());
echo $this->db->last_query();
What is the issue above query ?
I am getting below issue related to query
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 'WHERE `company_database`.`cdb_customer_id` = '19' AND `company`.`company_delete_' at line 2
SELECT * WHERE `company_database`.`cdb_customer_id` = '19' AND `company`.`company_delete_status` = 0
Filename: controllers/Test.php
Line Number: 112
You don't have a "from" clause in your where clause.
select * {from company} where 'company_database'.'cdb_customer_id' = ....
I suspect that the function
$this->ion_auth->is_customer()
may be calling another DB query and that pretty much completes the query you started above and once completed it does the $this->db with just the where clauses after.
To fix call the $this->ion_auth->is_customer() before you do $this->db->select and then in the IF statement simply just use the boolean returned so you don't
make another call to a query while you form another query.
Example:
--ADD THIS LINE
$bIsClient = $this->ion_auth->is_customer();
$this->db->select('company.*, cities.name as company_city, states.name as company_state, countries.name as company_country');
$this->db->from('company as company');
$this->db->join(CITIES.' as cities','cities.id = company.company_city_id' ,'left');
$this->db->join(STATES.' as states','states.id = company.company_state_id' ,'left');
$this->db->join(COUNTRIES.' as countries','countries.id = company.company_country_id' ,'left');
$this->db->join(COMPANY_DATABASE.' as company_database','company_database.cdb_company_id = company.company_id' ,'left');
--AND CHANGE THIS
if($bIsClient)
$this->db->where('company_database.cdb_customer_id',$this->session->userdata('user_id'));
$this->db->where('company.company_delete_status',NOT_DELETED);
$query = $this->db->get();
echo '<pre>';
echo $this->db->get_compiled_query();
print_r($query->result());
echo $this->db->last_query();

mysql query error when have '(' but no ')'

$q = "select * from product where decription = ?";
$param = 'package ( 2 chicken wings, 3 salad';
$result = DB::select($q, array($param));
there is an error query because the param string don't have ')'..
how to prevent query if there is '(' but no ')' in string?
error message : Syntax error or access violation: 1064 syntax error, unexpected $end
Look, you dont have $query.. that would be $q not $query
The answer is $result = DB::select($q, array($param));

eloquent - query builder running raw sql with bindings

I have a raw sql query code that I want to run using laravel query builder out of laravel :
$q = "SELECT * FROM wp_posts WHERE post_parent = ? ORDER BY ? ? LIMIT ?, ?";
$values = ['40','post_status', "ASC" ,'1','10'];
$q = $db->connection()->select($q,$values);
This query will throw an error saying that
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 '? LIMIT ?, ?' at line 1 (SQL:
SELECT * FROM wp_posts WHERE post_parent = `40` ORDER BY `post_status` ASC LIMIT 1, 10)
Now, when I copy the query from the error output and paste it in phpmyadmin sql runner it will run without any error .
Any Idea ??
could be post_parent is not a string and don't need backtics
$q = "SELECT * FROM wp_posts WHERE post_parent = ? ORDER BY ? ? LIMIT ?, ?";
$values = [ 40,'post_status', "ASC" ,'1','10'];
If you want to do it with query builder this should work.
$posts = DB::table('wp_posts')->where('post_parent', 40)->orderBy('post_status', 'ASC')->take(10)->get();
Laravel will automatically bind values with PDO.
It seems query builder does not allow to put SQL Commands in binding variables, when I removed 'ASC' out of binding variables it worked :
$q = "SELECT * FROM wp_posts WHERE post_parent = ? ORDER BY ? ASC LIMIT ?, ?";
$values = [40,'post_status', 1,10];
$q = $db->connection()->select($q,$values);
You Can write
$q = "SELECT * FROM wp_posts WHERE post_parent = ? ORDER BY ? ? LIMIT ?, ?";
$values = [ 40,'post_status', "ASC" , 1, 10];
hOPEFULLY THIS WILL SOLVE YOUR Problem

wordpress plugin sql synatax

problem in Mashable Slider Clone plugin when uload it in server
WordPress database 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 3] SELECT * FROM wp_mash_fields WHERE docid IN()
code for this is
$sql = "SELECT *
FROM $this->flds
WHERE docid IN(".implode(',' , array_keys($r)).")";
$r2 = $this->db->get_results($sql, ARRAY_A);
WordPress database 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 'LIMIT 0,999' at line 3] SELECT SQL_CALC_FOUND_ROWS DISTINCT wp_mash_documents.* FROM wp_mash_documents WHERE wp_mash_documents.type='image' ORDER BY wp_mash_documents. LIMIT 0,999;
code for this is
function get($type, $page = 0, $limit = 10, $sort = 'modify_time', $ord = 'ASC', $rel = null, $dorder = false, $s = null)
{
$ll = $page * $limit;
$docs = $this->docs;
$flds = $this->flds;
$rels = $this->rels;
$inner = array();
$where = array();
$order = '';
// get ids
$sql = "SELECT SQL_CALC_FOUND_ROWS DISTINCT $docs.*".($dorder? ",$rels.dorder" : "")." FROM $docs";
switch ($sort) {
case "title":
$inner[$flds] = array("$docs.id", "$flds.docid");
$where["$flds.name"] = "='title'";
$order = "$flds.value_text $ord";
if (isset($s)) {
$where["MATCH ($flds.value_text)"] = " AGAINST ('$s')";
}
Given your error message:
ORDER BY wp_mash_documents. LIMIT 0,999;
^---missing field name

SQL statement not working SELECT 1064 Error in your SQL syntax

My SQL statement doesn't work...
Here is my variable:
$email="test#test.com";
These statements doesn't work :
$sql = "SELECT * FROM table WHERE email = $email";
$sql = 'SELECT * FROM table WHERE email = ' . $email;
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 '#test.com' at line 1
But if I use a string instead of the variable, it works:
$sql = "SELECT * FROM table WHERE email = 'test#test.com'";
What's wrong with my statement?
Thanks!
please escape strings in mysql
$sql = "SELECT * FROM table WHERE email = $email";
$sql = 'SELECT * FROM table WHERE email = ' . $email;
This should work
$sql = "SELECT * FROM table WHERE email = '$email'";
table is a keyword, so can write like this
$sql = "SELECT * FROM `table` WHERE email='$email'";
$this->db->where('email', $this->input->post('email'));
$query =$this->db->get($this->user);
//$sql = "SELECT * FROM user WHERE email = '$email'";
if($query->num_rows == 1)
{
return true;
}
else
{
return false;
}
}
}