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'
Related
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.
I am trying to create an update query and I am looping in some set stuff to a var called $str and I cant seem to get it to work.
if (is_numeric($id)) {
if (!empty($values) && !empty($table_name)) {
$str = '';
$sql = "UPDATE `$table_name` SET :update_values WHERE `$column_name` = :id";
// Its one because we dont use ID like that
$i = 1;
foreach ($values as $key => $value) {
if ($key != $column_name) {
// Exclude the last one from having a comma at the end
if ($i == count($values) - 1) {
$str .= "$key='" . $value . "'";
} else {
$str .= "$key='" . $value . "', ";
$i++;
}
}
}
$query = $this->dbh->prepare($sql);
$query->bindValue('update_values', $str, PDO::PARAM_STR);
$query->bindValue(':id', $id, PDO::PARAM_INT);
$query->execute();
return true;
} else {
return false;
}
} else{
return false;
}
}
Output:
Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or
access violation: 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 ''note_name=\'yeet\', note_date=\'2020-02-20\',
note_desc=\'asdasdasdasdadsasdads' at line 1
Am I making any obvious mistakes?
Also for the life of me I don't know what the backslashes in front of the values mean.
In MySQL, identifiers cannot be provided as values.
References to columns must appear in the text of the SQL statement, they cannot be provided through bind parameters. This holds true for table names, column names, function names.
There is no workaround; this is a by-design restriction. There's several reasons for this. One of the most straightforward reasons is understanding how a SQL statement gets prepared, the information that is needed to come up with an execution plan, the tables and columns have to be known at prepare time (for the semantic check and privilege check. The actual values can be deferred to execution time.
Bind placeholders are for providing values, not identifiers.
With the code given, what MySQL is seeing something along the lines of
UPDATE `mytable` SET 'a string value' WHERE `id_col` = 42
And MySQL is balking at the 'a string value'.
We can (and should) use bind parameters for values.
We could dynamically generate SQL text that looks like this:
UPDATE `mytable`
SET `col_one` = :val1
, `col_two` = :val2
WHERE `id_col` = :id
and after the SQL text is prepared into statement, we can bind values:
$sth->bindValue(':val1', $value_one , PDO::PARAM_STR );
$sth->bindValue(':val2', $value_two , PDO::PARAM_STR );
$sth->bindValue(':id' , $id , PDO::PARAM_INT );
and then execute
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'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;