Unknown column error produced by Doctrine Query Where clause - php

I have the following query:
return Doctrine_Query::create()
->from('Model_Article m')
->where($where)
->orderBy($order);
In $where I have this:
$where='m.title='.$name;
The above produces this error:
Message: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'sadsa' in 'where clause'. Failing Query: "SELECT COUNT(*) AS num_results FROM article a WHERE a.title = sadsa"
Why?

Error is because resulting query looks like "** where title=sadsda **", so SQL engine looks for column named "sadsda". To prevent it you must mention, that you want to compare with a string, not a column value. You can make it using your engine rules (usually enclose string with "'"), but it isn't secure, I think you should use placeholders engine, that doctrine provides, for example
$whereKey = 'm.title=?';
$whereValue = 'sadsda';
Doctrine_Query::create()->from('Model_Article m')->where($whereKey,$whereValue)->orderBy($order);

Related

how to solve unknown where class error 1054 ( at error number)

I'm implement select query in codeigniter application.
But it shows the below error.
A Database Error Occurred
Error Number: 1054
Unknown column 'Ak3456' in 'where clause'
SELECT * FROM `table` WHERE `Ak3456` IS NULL
Filename: C:/wamp/www/application/system/database/DB_driver.php
Line Number: 691
My select query is
$data = $this->db->get_where('table',$number);
Please any one help me to solve this problem.
The proper syntax is
// replace table with your table name and field_name with for which value
//you want to search for Ak3456
$data = $this->db->get_where('table',['field_name' => 'Ak3456']);
Doc link.
Syntax
get_where([$table = ''[, $where = NULL[, $limit = NULL[, $offset = NULL]]]])
Parameters
$table (mixed) – The table(s) to fetch data from; string or array
$where (string) – The WHERE clause
$limit (int) – The LIMIT clause
$offset (int) – The OFFSET clause
Returns: CI_DB_result instance (method chaining)
Return type: CI_DB_result
In codeigniter select query structure is below,
$this->db->get_where('table_name',['feild_name'=>'value']);
So you can provide where data as in array type
For example,
$data = $this->db->get_where('table',array('feild_name' => $number));

Count in update query doesn't work in Laravel

public function increment($id)
{
$this->model->where("id",'=', $id)->update(['rating'=> DB::raw('count+1')]);
}
I am getting the following error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'count' in
'field list' (SQL: update news set rating = count+1, updated_at
= 2019-04-13 08:12:51 where id = 5)
I also tried
->update(['rating'=>'count+1']);
You are not telling the query builder on which table you are performing the query, so DB::raw('count+1') makes no sense.
You can try to use the eloquent increment method like this:
$this->model->where("id", $id)->increment('rating');
Thanks #Tharaka removed the extra call to save().

Column not found: 1054 Unknown column 'locations.id' in 'on clause

I'm trying to load activities from a database with a specific location. I try to do that with this query:
public function selectAllActivities(){
$sql = "SELECT * FROM `activities` INNER JOIN `locations` on `activities`.`location_id` = `locations.id`";
$stmt = $this->pdo->prepare($sql);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
However when I load the website, I get this error:
Column not found: 1054 Unknown column 'locations.id' in 'on clause
That's a typo, but I can't explain it in comment because of the backticks.
This :
`locations.id`
Is meant to be
// v-v------- Notice the backticks
`locations`.`id`
Have you tried
`activities`.`location_id` = `locations`.`id`
Inner joins should read like
ON table1.column_name = table2.column_name;
You can read more at this URL

Doctrine substring field with alias in where clause

I have the following query:
$query = $qb->select($qb->expr()->substring("p.website",1,5).'AS country)
->from("AppBundle\Entity\Image" ,"p")
->where("p.aktuellste = 1")
->andWhere($qb->expr()->in('country',':country'))
->setParameter(':country','de')
->orderBy('country','DESC')
->getQuery();
E.g. I would like to select all lines where substring(1,2) of website is de. But country is an unknown column for Doctrine in the WHERE clause. The following exception is thrown:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'sclr_0' in
'where clause
The funny thing is that Doctrine knows the country column in the ORDER BY clause.
Does anybody know how to fix this?
If you want to search on an aliased column, you need to use HAVING, because that is on a calculated field. Putting it in your WHERE clause is assuming that your Image entity has a member named country in it, which it does not.
$query = $qb
->select($qb->expr()->substring('p.website', 1, 5).'AS country')
->from('AppBundle\Entity\Image', 'p')
->andWhere('p.aktuellste = 1')
->andHaving($qb->expr()->in('country', ':country'))
->setParameter(':country', 'de')
->orderBy('country', 'DESC')
->getQuery()
;
See MySQL Handling of Group By for more information:
The MySQL extension permits the use of an alias in the HAVING clause
for the aggregated column
There are some syntax errors in your query, but not sure this fixes your problem.
Please try this:
$query = $qb->select($qb->expr()->substring("p.website",1,5).'AS country')
->from("AppBundle\Entity\Image" ,"p")
->where("p.aktuellste = 1")
->andWhere($qb->expr()->in('country=:country'))
->setParameter('country','de')
->orderBy('country','DESC')
->getQuery();

Column not found:1054 error even though the column is defined in laravel

SQLSTATE[42S22]: Column not found: 1054 Unknown column
'vpc_order_info' in 'where clause' (SQL: select `sequa_transaction_id`
as `vpc_transaction_no`, `merchant_order_id` as `vpc_order_info`,
`date_created` as `yesterday`, `card_number` as `vpc_Card`, `amount`
as `vpc_Amount`, `authorization_code` as `vpc_authorizeId` from
`ipg_sentry_response` where `merchant_id` = 12136901 and
`vpc_order_info` like %-% and `response_code` = 1 and `date_created`
between 2016-03-22 and 2016-09-31)
I am getting this error message though I have defined the column as given below.
$col = array("sequa_transaction_id as vpc_transaction_no","merchant_order_id as vpc_order_info","date_created as yesterday","card_number as vpc_Card", "amount as vpc_Amount","authorization_code as vpc_authorizeId");
$records = DB::table('ipg_sentry_response')->select($col)
->where('merchant_id', '=', $vpc_Merchant)
->where('vpc_order_info' ,'like', "%-%")
->where('response_code', '=', '1')
->whereBetween('date_created', array($date,$date2))
//->whereBetween('date_created', array($date,$date2))
->get();
Can anyone please help me with it? I am using Laravel version 5.2.
The column alias is defined but it is not available to be used in where caluse yet. You can use alias in group, order... you need to use the column name instead of the alias. Try -
$col = array("sequa_transaction_id as vpc_transaction_no",
"merchant_order_id as vpc_order_info","date_created as yesterday",
"card_number as vpc_Card", "amount as vpc_Amount",
"authorization_code as vpc_authorizeId");
$records = DB::table('ipg_sentry_response')->select($col)
->where('merchant_id', '=', $vpc_Merchant)
->where('merchant_order_id' ,'like', "%-%")
->where('response_code', '=', '1')
->whereBetween('date_created', array($date,$date2))
->get();
You can only use column aliases in GROUP BY, ORDER BY, or HAVING clauses. Standard SQL doesn't allow you to refer to a column alias in a WHERE clause.
As what stated by the answer above mine, the WHERE clause doesn't accept aliases. Furthermore, you may also use the GROUP BY - HAVING methods (https://laravel.com/docs/5.3/queries#ordering-grouping-limit-and-offset)

Categories