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

$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));

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();

PDO statement error #1064

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');

MySQL syntax error, for no apparent reason

I have this MySQL statement writen in PHP, but it seems to contain a syntax-error.
$user_forum_sql = ( !empty($forum_id) ) ? " WHERE session_page = " . intval($forum_id) : '';
$sql = "SELECT * FROM " . $session_table_name . '"'.$user_forum_sql.'"';
This is the error I'm having. I'm not sure what is causing it.
SQL Error : 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
SELECT * FROM ""
$user_forum_sql = ( !empty($forum_id) ) ? " WHERE session_page = " . intval($forum_id) .'' ;
$sql = "SELECT * FROM " . $session_table_name.$user_forum_sql;
echo $sql;
Your $session_table_name is empty, so you might have not started your session or have not set the value.

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