Symfony2 error Expected end of string - database

Trying to run a query with symfony and I get this error:
[Syntax Error] line 0, col 83: Error: Expected end of string, got 'username'
This code throws that error:
$query = $em->createQuery(
'SELECT username
FROM BLOGBlogBundle:user
WHERE username= :usrname'
)->setParameter('usrname', $usr);
$products = $query->getResult();
What am I doing wrong?

Seems to work when you add in the alias
$query = $em->createQuery('
SELECT u.username
FROM BLOGBlogBundle:user u
WHERE u.username = :usrname')
->setParameter('usrname', $usr);
$products = $query->getResult();

What worked for me is using Exp with ->set() like $qb->expr()
$qb = $this->em->createQueryBuilder();
$q = $qb->update('models\User', 'u')
->set('u.username', $qb->expr()->literal($username))
->set('u.email', $qb->expr()->literal($email))
->where('u.id = ?1')
->setParameter(1, $editId)
->getQuery();
$p = $q->execute();

Related

How to bind pdo key and value from Question mark?

I want to make a query select data :
$pdo = new PDO($dsn, $user, $pass, $options);
$query = "SELECT ? FROM ? WHERE ? = ?";
$pdo->prepare($query);
How can I do it?
PDOpreapare statement is not support this query right row

psycopg2 - TypeError: 'int' object is not subscriptable

I'm performing the postgres query with psycopg2, stored in function columnsearch(). My aim is to be able to view the data in each field by using the column name in the return statement. I'm receiving the error:
TypeError: 'int' object is not subscriptable
Does anyone know how I can rectify this?
def columnsearch():
con = psycopg2.connect(connectstring)
cur = con.cursor(cursor_factory=e.DictCursor)
cur.execute("SELECT * FROM radio_archive_ind.radio_archive_ind WHERE tape_number = 'TP00001'")
rows = cur.fetchone()
for r in rows:
return r["tape_number"]
print columnsearch()
Note that .fetchone() returns a single sequence so you can just do:
def columnsearch():
con = psycopg2.connect(connectstring)
cur = con.cursor(cursor_factory=e.DictCursor)
cur.execute("""SELECT *
FROM radio_archive_ind.radio_archive_ind
WHERE tape_number = 'TP00001'""")
row = cur.fetchone()
return row["tape_number"]

How to check whether value is in database without throwing an exception in Zend?

I have a registration action in Zend controller and I want to ensure that given username isn't already in the database.
$tUser = $userMapper->getDbTable();
$select = $tUser->select()
->from(array('u'=>'users'))
->where('u.user_username = ' . $value);
$row = $tUser->fetchRow($select);
Unfortunately, this snippet of code throws the Exception when i just want to check the $row is null or not and keep writing my code.
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'admin' in 'where clause', query was: SELECT u.* FROM users AS u WHERE (u.user_username = admin) LIMIT 1
Should I remove the try/catch blocks in my controller or can I do it in another way?
Judging by error description, I'd say it will throw an exception always, no matter if user exists or not, because bad SQL query will be generated. Try passing $value as second parameter to where, like this:
$tUser = $userMapper->getDbTable();
$select = $tUser->select()
->from(array('u'=>'users'))
->where('u.user_username = ?', $value);
$row = $tUser->fetchRow($select);
You will then check if user exists like this:
if(!$row) {
// user doesn't exist
} else {
// user exists
}
try {
$tUser = $userMapper->getDbTable();
$select = $tUser->select()
->from(array('u'=>'users'))
->where('u.user_username = ?', $value);
$row = $tUser->fetchRow($select);
} catch (Zend_Db_Exception $e) {
echo "<pre>";print_r($e);
}

Generate wrong "Update" query cakephp

I am trying to update data using below code but it is generating wrong SQL query. don't understand why it is generating wrong one. Please help me out.
$data = array();
$data['Pers']['etat'] = 1;
$data['Pers']['Activation'] = '';
$this->Pers->id = $results['Pers']['persID'];
$this->Pers->save($data);
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'bdr+.pers.persID' in
'where clause'
SQL Query: UPDATE `bdr+`.`pers` SET `etat` = 1, `Activation` = '',
`date_time` = '2014-03-19 11:33:21' WHERE `bdr+.pers.persID` = 37
It means bdr+.pers.persID is generating wrong. Don't understand why it is generating like this.
check if this $results['Pers']['persID'] variable if it is returning a proper format
then try this
$data = array();
$data['Pers']['persID'] = $results['Pers']['persID'];
$data['Pers']['etat'] = 1;
$data['Pers']['Activation'] = '';
//Just to make sure your model is loaded
$this->loadModel('Pers');
if ($this->Pers->save($data, false)) {
echo 'Pres has been saved :) ';
}
Your code containing bdr+.pers.persID` = '37' because $results['Pers']['persID'] supplying 37

How to add IN statement with JOIN ON clause in tablegateway ZF2

Getting all result so far until I added a 'IN' statement with join ON clause
My code is something like this:
// $this->table = 'category';
$sql = $this->getSql();
$select = $sql->select();
$select->join('user_category_subscriptions', 'user_category_subscriptions.category_id = category.id AND user_category_subscriptions.status IN (1,2,3,4)', array(), 'left');
$select->where(array('category.user_id = 2 OR (user_category_subscriptions.status IN (2,4))'));
Error
ZEND\DB\ADAPTER\EXCEPTION\INVALIDQUERYEXCEPTION
File:E:\htdocs\myproj\vendor\zendframework\zendframework\library\Zend\Db\Adapter\Driver\Pdo\Statement.php:245
Message:Statement could not be executed
Used 'echo $select->getSqlString();' to print the query:
SELECT "categories".* FROM "categories"
LEFT JOIN "user_category_subscriptions"
ON "user_category_subscriptions"."category_id" = "categories"."category_id" AND "user_category_subscriptions"."status" IN ("1""," "2""," "3""," "4")
WHERE category.user_id = 2 OR (user_category_subscriptions.status IN (2,4))
So the problem is zend's auto converting (1,2,3,4) into ("1""," "2""," "3""," "4")
Any idea to solve this? Thanks
I think you can pass an expression instead of the join string:
// $this->table = 'category';
$sql = $this->getSql();
$select = $sql->select();
$join = new Expression('user_category_subscriptions.category_id = category.id AND user_category_subscriptions.status IN (1,2,3,4)');
$select->join('user_category_subscriptions', $join, array(), 'left');
$select->where(array('category.user_id = 2 OR (user_category_subscriptions.status IN (2,4))'));
try this
$select->where
->AND->NEST->equalTo('category.user_id', 2)
->addPredicate(new Sql\Predicate\In('user_category_subscriptions.status', array(2,4)));

Resources