Here is my code.
public function get_records($event_id = null)
{
$this->db->select('a.*');
$this->db->from('attendee a');
$this->db->join('event e','e.event_id = a.attendee_event_id','left');
$this->db->join('users u','u.id = e.event_user_id','left');
if($event_id != null)
$this->db->where('a.attendee_event_id',$event_id);
if($this->ion_auth->is_agent())
{
$this->db->where('e.event_user_id', csession('user_id'));
}
else
{
$this->db->group_start();
$this->db->where('e.event_user_id', csession('user_id'));
$this->db->or_where('u.created_by', csession('user_id'));
$this->db->group_end();
}
$query = $this->db->get();
return $query->result();
}
for this code i am getting below result
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 ( `e`.`event_user_id` = '93' OR `u`.`created_by` = '93' ) AND `a`.`atte' at line 2
SELECT * WHERE ( `e`.`event_user_id` = '93' OR `u`.`created_by` = '93' ) AND `a`.`attendee_event_id` = '2'
Filename: models/Attendee_model.php
Line Number: 85
Whenever i am passing event_id, it's not working fine.
but when I am not passing event_id, it's working fine.
I don't know what i am doing wrong here.
CI is throwing that error because you are not providing the table name in the "get()" method.
Your code
$query = $this->db->get();
Correct approach
$query = $this->db->get('main_table_name');
For a better understanding of CI Query Builder read here
$this->db->group_start();
$this->db->where('e.event_user_id', csession('user_id'));
$this->db->or_where('u.created_by', csession('user_id'));
$this->db->group_end();
I have removed above code and it started working. btw it was unnecessary code. But I coudn't find the issue with this code though.
Related
I am using zend expression in my query.
This is my query.
$oResultSet = $this->select(function (Select $select) use ($aWhere, $sGroup) {
$select->columns(array('next_level_name' => new Expression("SELECT level_title from mry_game_sublevel WHERE sub_level = mry_game_sublevel.next_level LIMIT 1")));
//$select->columns(array('count' => new \Zend\Db\Sql\Expression('COUNT(*)')));
$select ->join(
'mry_game_main_level',
'mry_game_sublevel.main_level_id = mry_game_main_level.main_level_id' ,
array('main_level','level_name'),
$select::JOIN_LEFT
);
if ($aWhere) {
foreach ($aWhere as $key => $value) {
$select->where($key.$value);
}
}
if ($sGroup) {
$select->group($sGroup);
}
});
return ! $oResultSet ? false : $oResultSet->toArray();
But I got an error message for this.
My mysql query is like this
select ee.main_level_id,
(select sub_level from mry_game_sublevel where next_unlock = ee.sub_level) as dddd
from mry_game_sublevel as ee where ee.sub_level = 1.2
The error message:
Statement could not be executed (42000 - 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 'SELECT level_title from mry_game_sublevel WHERE sub_level = mry_game_sublevel.ne' at line 1)
Does anyone know what is the issue?
I'm trying to bind a search term and a limit value to a PDO execute query, but I get error messages no matter which way I do it
public static function searchUsersByName($searchTerm, $results = null) {
//getDBConnection
if($results == null) {
$results = 5;
}
$searchTerm = '%'.$searchTerm.'%';
$query = $database->prepare("SELECT user_id, user_firstname, user_lastname
FROM users_details
WHERE user_firstname LIKE :searchTerm
OR user_lastname LIKE :searchTerm
LIMIT :results");
$query->bindParam(':searchTerm', $searchTerm, PDO::PARAM_STR);
$query->bindParam(':results', $results, PDO::PARAM_INT);
$query->execute();
$search_results = array();
foreach ($query->fetchAll() as $user) {
$search_results[$user->user_id] = new stdClass();
$search_results[$user->user_id]->user_id = $user->user_id;
$search_results[$user->user_id]->user_firstname = $user->user_firstname;
$search_results[$user->user_id]->user_lastname = $user->user_lastname;
}
return $search_results;
}
This is the error I get from this:
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 "5"
It works fine if I take out the bind for LIMIT and just hardcode 5 into the SQL query, but I want to be able to change it if possible
$query->execute(array(':searchTerm' => '%'.$searchTerm.'%', ':results' => $results));
I've tried doing it this way, but of course PDO automatically puts quotes around the values its inserting via this method, and as far as I know you can't put a PDO::PARAM_INT in while using this method.
What am I doing wrong?
Could it be that $results is not an integer? The error seems like your PHP code is posting a string into the query, which would explain the error.
I am guessing this is the issue because of the following piece of code
if($results == null) {
$results = 5;
}
How is $results set in the first place? Via GET/POST? Then it might have been converted to a string.
I've tried your piece of code myself and casting it to an int fixed it for me.
$query->bindParam(':results', intval($results), PDO::PARAM_INT);
I am trying to convert this D6 code with query to Drupal 7
foreach ($order->products as $product) {
if (db_result(db_query("SELECT nid FROM {the_table} WHERE fid = 'string' AND nid = %d", $product->nid))) {
$nid[] = $product->nid;
}
}
I changed it to for the query:
if (db_result(db_query('SELECT nid FROM {the_table} WHERE fid = 'string' AND nid = :nid', array(':nid' => $product->nid)))) {
AND then to as a Dynamic query it came out written as this I thought
foreach ($order->products as $product) {
$query = db_select('{the_table}', '');
$query->fields('nid', array(''));
$query->condition('fid', 'string');
$query->condition('nid', ':nid');
$query->execute();
$result = $query->fetchAssoc();
foreach ($result as $record) {
$product->nid;
$nid [] = $product->nid;
}
}
I also tried a "->fetchfield();" statement in there instead - same error
I also tried :string instead of 'string'
It throws this error
PDOException: 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 'AS FROM the_table the_table WHERE (fid = 'string') AND' at line 1: SELECT nid. AS FROM {the_table} the_table WHERE (fid = :db_condition_placeholder_0) AND (nid = :db_condition_placeholder_1) ; Array ( [:db_condition_placeholder_0] => string [:db_condition_placeholder_1] => :nid ) in process() (line 450 of /. . . the.module).
ALSO I do not know why the error shows two of "the_table" - one behind another - so is it possible the otrignal D6 query was bad ??
Does anyone know what I have done wrong ??
I did not pay close enough attention that the brackets are not proper syntax in the new format of query. It works - and i see the other places I went wrong after reading the dynamic query page again at https://www.drupal.org/dynamic-queries
<?php
$query = db_select('the_table', 'the_table');
$query->fields('the_table', array('nid'));
$query->condition('the_table.fid', 'string');
$query->condition('the_table.nid', $product->nid);
$query->execute();
?>
You should fix the following errors:
// error
$query = db_select('{the_table}', '');
// fixed
$query = db_select('TABLE_NAME', 'TABLE_ALIAS'); // remove the curly braces. The second argument is for you to choose an alias for your table.
// error
$query->fields('nid', array(''));
// fixed
$query->fields('nid', array('id', 'name')); // Choose the fields you want to return from the query.
// error
$query->condition('nid', ':nid');
// fixed
$query->condition('nid', $someVariable, '='); // no need to use ':nid'
I'm using this code to select some rows with date less than a certain day. When I put <= (less than) in the sql in throws me a fatal error. It works (without error but wrong results) when only adding =.
I'm using PHP-MySQLi-Database-Class from https://github.com/ajillion/PHP-MySQLi-Database-Class
$params = array($id, $day, $day);
$results = $db->rawQuery("
SELECT *
FROM `forecasts`
WHERE `geonameid`=? and (DAY(time_from) <= DAY(?)
OR DAY(time_to) <= DAY(?))", $params);
Fatal error: Problem preparing query (SELECT * FROM forecasts WHERE geonameid=? and (DAY(time_from) ) 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 ''
What I am doing wrong?
Looks like it's PHP-MySQLi-Database-Class problem.
Line 101, function rawQuery
$this->_query = filter_var($query, FILTER_SANITIZE_STRING);
this function strip tags, and result for your query is
SELECT * FROM `forecasts` WHERE `geonameid`=? and (DAY(time_from)
so you can just replace this line with
$this->_query = $query;
For some reason this SQL statement is not working. Can anyone tell me why? (This is a Codeigniter site, if that matters)
Here is my Model (where my error is pointing me to)
public function edit_profile($ID, $field, $new_info)
{
$sql = "UPDATE users SET ?=? WHERE id=?";
$query = $this->db->query($sql, array($field, $new_info, $ID)); // <<<< LINE 42
return $query;
}
And this is the error I'm getting
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 ''first_name'='oooo' WHERE id='151'' at line 1
UPDATE users SET 'first_name'='oooo' WHERE id='151'
Filename: /Applications/MAMP/htdocs/MY_SITE/models/member_model.php
Line Number: 42
My table is called 'users', and I have a 'first_name' and 'id' column.
Ideas?
EDIT
Just because it seems to come up a bit I want to clarify that the variables I am passing in here have NO QUOTES OR BACKTICKS. They are being added somewhere (and it seems like the ->query method, but I cant imagine that's true? .. dunno though, cause it's my first CI project)
Here is the controller that is passing to the model...
public function profileEdit()
{
$ID = $this->the_user->id;
$field = $this->input->post('edit_field')
$field = strstr($field,'_edit', true);
$new_info = $this->input->post('new_info');
$this->load->model('Member_model');
if( $this->Member_model->edit_profile( $ID, $field, $new_info )){
echo 'success';
}
else
{
echo 'error';
}
}
i suggest use like this :
public function edit_profile($ID, $field, $new_info)
{
$sql = "UPDATE users SET $field =? WHERE id=?"; # UPDATED (remove $this->db->escape())
$query = $this->db->query($sql, array($new_info, $ID)); // <<<< LINE 42
return $query;
}
if $field is not secured you can use escape functions .
EDIT :
$this->db->escape() will add quotes around variable so you will get an error again .