zend subquery is not working - php

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?

Related

why it's generating query without "from tablename" in codeigntier

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.

SQLSTATE[42000]: Syntax error or access violation: 1064 Error

I am getting the following error on a website. I create ticket for this reason in my hosting provider. It told me "You need to edit the select query, not a select query suitable for the mariadb version on the server." they said.
error_log File:
[25-Dec-2021 19:50:24 Europe] PHP 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 'and dripfeed= 2' at line 1 in /home/user/public_html/script.php:461
Stack trace:
#0 /home/user/public_html/script.php(461): PDO->query('SELECT * FROM o...')
#1 /home/user/public_html/index.php(35): require_once('/home/user/...')
#2 {main}
thrown in /home/user/public_html/script.php on line 461
script.php File:
$dripfeedvarmi = $conn->query("SELECT * FROM demo WHERE user=$user_id and dripfeed=2");
if ($dripfeedvarmi->rowCount())
{
$dripfeedcount = 1;
}
else
{
$dripfeedcount = 0;
}
Current DB Version: 10.2.41-MariaDB-cll-lve
PHP Version: 7.4.25
OS: Linux
Thank you in advance for your help.
even if the MySQL syntax is correct, do not write code like this. Always prepare your query to make it secure!
Try this example:
$query = 'SELECT * FROM demo WHERE user = ? AND dripfeed = ?';
$array = array($user_id, 2);
$init = $conn->prepare($query);
$init->execute($array);
$rowCount = $init->rowCount();
if($rowCount > 0){
$dripfeedcount = 1;
}else{
$dripfeedcount = 0;
};
Also if you are storing the id of the user, so why the column name is not user_id instead of user? Be clean...
You can also try like this to execute the query using prepare() and execute() methods.
$dripfeedvarmi = $conn->prepare("SELECT * FROM demo WHERE user=:user and dripfeed=:dripfeed");
$dripfeedvarmi->execute([':user'=>$user_id,':dripfeed'=>2]);
if ($dripfeedvarmi->rowCount()>0)
{
$dripfeedcount = 1;
}
else
{
$dripfeedcount = 0;
}

1064 You have an error in your SQL syntax in Doctrine. Why?

I don't understand, why my browser return this error and why my sql doesn't work. I try resolved this problem, but I don't know, how can I do this.
private function getFromDb()
{
$queryBuilder = $this->connection->createQueryBuilder();
$query = $queryBuilder
->select('art1.articleID', 'art1.valueID')
->from('s_filter_articles', 'art1')
->innerJoin('art1', 's_filter_articles', 'art2', 'art1.articleID = art2.articleID')
->where('art1.valueID = 12499')
->andWhere('art2.valueID = 12500');
return $query->execute()->fetchAll();
}
This code return this array:
array:2 [▼
0 => array:2 [▼
"articleID" => "2225"
"valueID" => "12499"
]
1 => array:2 [▼
"articleID" => "2250"
"valueID" => "12499"
]
]
...and I would like to delete ALL this result in MySQL. SO I wrote this code:
public function removeFromDb()
{
$result = $this->getFromDb();
foreach ($result as $option) {
$queryBuilder = $this->connection->createQueryBuilder();
$query = $queryBuilder
->delete('s_filter_articles', 's')
->where('articleID = :articleID')
->andWhere('valueID = :valueID')
->setParameter(':articleID', $option['articleID'])
->setParameter(':valueID', $option['valueID']);
return $query->execute();
}
}
BUT my browser return this error:
> 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 MySQL server version for the right
> syntax to use near 's WHERE (articleID = '2225') AND (valueID =
> '12499')' at line 1 in
> /var/www/html/strefatenisa.local/vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php
> on line 131
> Doctrine\DBAL\DBALException: An exception occurred while executing
> 'DELETE FROM s_filter_articles s WHERE (articleID = ?) AND (valueID =
> ?)' with params ["2225", "12499"]: 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 's WHERE (articleID = '2225') AND (valueID =
> '12499')' at line 1 in
> /var/www/html/strefatenisa.local/vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php
> on line 131
EDIT:
If I do normal query in my mysql, all works good.
SELECT art1.articleID, art1.valueID
FROM s_filter_articles as art1
INNER JOIN s_filter_articles as art2 ON art1.articleID = art2.articleID
WHERE art1.valueID = 12499
AND art2.valueID = 12500
DELETE FROM `s_filter_articles` WHERE `articleID` = 2225 AND `valueID` = 12499
Update your function removeFromDb() with this code:
public function removeFromDb()
{
$result = $this->getFromDb();
foreach($result as $option){
$queryBuilder = $this->connection->createQueryBuilder();
$queryBuilder->delete('s_filter_articles', 's')
->where('articleID = :articleID')
->andWhere('valueID = :valueID')
->setParameter(':articleID', $option['articleID'])
->setParameter(':valueID', $option['valueID']);
$query = $queryBuilder->getQuery();
$query->execute();
}
}
createQueryBuilder function returns a new instance of the QueryBuilder class which doesn't have any declaration of the execute() method. That's why I added the getQuery() method to return the Query class which is the one that has the execute() method. Another thing that I removed is the return from inside of the foreach. I hope this work for you, if not let me know and I can help you.

db_result D6 query conversion to db_select D7

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'

Yii CDbCommand failed to execute the SQL statement: SQLSTATE[42000]:

I get this error when I didn't do anything for a while, I'm not sure if this is a Session problem or not.
The error message is:
CDbCommand failed to execute the SQL statement: 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 '=='1')' at line 1. The SQL statement executed was: SELECT * FROM games_developers_app t WHERE (status LIKE :ycp0) AND (developer_id==:ycp1)
The code is:
public function actionSortReject(){
$util = new Utility();
$util->detectMobileBrowser();
$util->checkWebSiteLanguageInCookies();
$this->layout = "masterLayout";
$count = '0';
$id = Yii::app()->user->getState('id');
$searchsort = "REJ";
$sort = new CDbCriteria();
$sort->addSearchCondition('status', $searchsort);
$sort->addCondition('developer_id='.$id);
$models = GamesDevelopersApp::model()->findAll($sort,array('developer_id'=>$id));
$this->render('/register/applist',array('models'=>$models,'count'=>$count));
}
It seems that everything worked fine, if I missed something in my code please tell me. Thanks =)
The problem is a combination how you have called compare and added additional parameters to findAll.
Your compare should be as follows:
$sort->compare('developer_id', $id);
And your findAll should be:
$models = GamesDevelopersApp::model()->findAll($sort);
You could also use addCondition as follows:
$sort->addCondition('developer_id=:developer_id');
$sort->params[':developer_id'] = $id;

Categories