After long research I could not find a exact solution for the PDOException SQLSTATE[HY093]
I am using yii 2 frame work and i have problem when using between condition.
$query = SalesOrder::find()->where(['between','created_date', ':fromDate', ':toDate']);
$query->addParams([':fromDate' => $salesReport->fromDate,':toDate' => $salesReport->toDate]);
$query->all();
tried differently same problem.
$qString1=':fromDate';
$qString2=':toDate';
$query = SalesOrder::find()->where(['between','created_date',$qString1,$qString2]);
$query->addParams([':fromDate' => $salesReport->fromDate],[':toDate' => $salesReport->toDate]);
$query->all();
Caused by: PDOException
SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens
You can not pass parameter name in this case. try :
$query = SalesOrder::find()->where(['between','created_date', $salesReport->fromDate, $salesReport->toDate]);
$query->all();
Or, If you'd like to use parameter, try :
$query = SalesOrder::find()->where('created_date BETWEEN :fromDate AND :toDate');
$query->addParams([':fromDate' => $salesReport->fromDate],[':toDate' => $salesReport->toDate]);
$query->all();
Related
I have this query that without the inner join it works:
$sql = 'SELECT prodotti.nome, prodotti.prezzo, prodotti.sku, prodotti.produttore, fornitori.nome
FROM prodotti INNER JOIN fornitori
ON prodotti.fornitori_id = fornitori.id
WHERE prodotti.id = :prodotti.id';
$id = 1; // for example
// $this->db-> (is connection)
$stmt = $this->db->prepare($sql);
$stmt->bindParam(':prodotti.id', $id, PDO::PARAM_INT);
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
$prodlist[$id] = $results;
var_dump($prodlist);
If I run it I get this error:
Fatal error: Uncaught PDOException: SQLSTATE [HY093]: Invalid parameter number: parameter was not defined in ........
It seems that the error lies is in the WHERE and in a particular way, reading online, I discovered that it could be a problem to do this: WHERE prodotti.id = prodotti.id;
suggest to do for example: WHERE prodotti.id =: id '; and then in the bindparam the same thing $stmt->bindParam(': id', $ id, PDO :: PARAM_INT);
in fact, doing so works halfway, in the sense that it gives me back the data called the first 'products' table ignoring the second 'suppliers'.
Can anyone tell me where am I wrong? Thanks
if I run this query on the SQL section of DB it works.
SELECT prodotti.nome, prodotti.prezzo, prodotti.sku, prodotti.produttore, fornitori.nome
FROM prodotti INNER JOIN fornitori
ON prodotti.fornitori_id = fornitori.id
WHERE prodotti.id = 1
as some users have pointed out to me elsewhere, and in fact it partially solves the problem, the point cannot be used in the binding, as reported in the guide at this link:
https://phpdelusions.net/pdo
"Note that PDO supports positional (?) and named (:email) placeholders, the latter always begins from a colon and can be written using letters, digits and underscores only. Also note that no quotes have to be ever used around placeholders."
now it works correctly! Thanks
I got this error when using the code below (everything is in a try catch block):
SQLSTATE[HY093]: Invalid parameter number: number of bound variables
does not match number of tokens
$item_q = 4;
$item_no = 12;
$update = $db->prepare("UPDATE stock
SET quantity = quantity - :item_q
WHERE item_number = :item_no");
$update->execute([':item_q' => $item_q]);
$update->execute([':item_no' => $item_no]);
It is something with the :item_q that is causing problem and i do not what it is. It works when I replace the :item_q in the sql query with a number. I am happy for some help! :)
Issue
Currently you're executing twice with one parameter bound each time.
Solution
You want to either bind the parameters first then execute, or execute with both parameters bound.
Executing with both bound values in PDOStatement::execute() function
$update->execute([":item_q" => $item_q, ":item_num" => $item_no]);
Executing after binding parameters with PDOStatement::bindParam() function
$update->bindParam(":item_q", $item_q, PDO::PARAM_INT);
$update->bindParam(":item_num", $item_no, PDO::PARAM_STR);
$update->execute();
Problem is with executing your statement two times.
$update->execute([':item_q' => $item_q]);
$update->execute([':item_no' => $item_no]);
Combine it into one as
$update->execute([':item_q' => $item_q,':item_no' => $item_no]);
Try this and let me know if it doesn't work
$update->execute([':item_q' => $item_q,':item_no' => $item_no]);
It's all the day that I'm stuck with this simple prepared statement:
// $conn it's my PDO Object
// and $intervention my params'array
$s = $conn->prepare("INSERT INTO intervention(firm_id,category,subject,amount,start_date,end_date) VALUES(:firm_id,':category',':subject',:amount,':start_date',':end_date')");
$result = $s->execute(array(
'firm_id' => $firm_id ,
'category' => $intervention["category"] ,
'subject' => $intervention["subject"] ,
'amount'=> $intervention["amount"] ,
'start_date'=> $intervention["start_date"],
'end_date'=>$intervention["end_date"]
));
The execute will give me:
Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: :category
Can someone help me understand what is wrong with this simple code?
In this part of the query:
VALUES(:firm_id,':category',
:category is taken as a literal string and not as a parameter name, because of the quotes enclosing it.
There should be no quotes around parameter names, as in:
...VALUES(:firm_id, :category,...
There is the same mistake for the other non-numeric parameters of the rest of the query.
Parameters name should not have a quotes. The prepared statement will do the replacement properly. Pay attention too at the number of parameters you write in the query and what will you bind on execute method.
For an SQL query involving multiple tables, how do I construct such PDO statement like this?
Because this doesn't work:
$stmt = $pdo -> prepare("UPDATE category, product
SET product.category_id = category.id,
product.xxx = :product.xxx,
category.yyy = :category.yyy
WHERE product.category_slug = category.slug
AND product.aaa = :product.aaa"
);
$stmt->execute(array(
'product.xxx' => '',
'category.yyy' => '',
'product.aaa' => ''
));
Which gives these errors:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]:
Invalid parameter number: parameter was not defined'
PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
How do I make this work? PDO doesn't seem to allow period dots in marked parameters? I guess I'm doomed with underscores?
Here are the allowed characters for named placeholders:
[:][a-zA-Z0-9_]+;
Alphanumeric and underscores.
Ref. https://github.com/php/php-src/blob/master/ext/pdo/pdo_sql_parser.re (this is the source)
This question already has an answer here:
MysQl error : Invalid parameter number
(1 answer)
Closed 8 years ago.
Its probley something small but i been looking at this for ages and still cant get it to work at all im getting two errors
Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in
Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number in
the code i have is this:
public function getdata ($tran_id)
{
$sql = "SELECT tran_id, seller_user_name, user_name_buyer
FROM trade_transaction, feedback Where feedback.feedback_username = trade.user_name_of_buyer
AND user_name_of_buyer = :user_name_buyer ";
$sth = $this->db->prepare($sql);
$sth->execute(array(':tran_id' => $tran_id, ':user_name_buyer ' => $_SESSION['user_name']));
$user = $sth->fetch();
You're binding a :tran_id parameter during your call to execute, but you're not using that parameter in your query.
Change your execute line to this
$sth->execute(array(':user_name_buyer ' => $_SESSION['user_name']));
Remove :tran_id from your parameter list or add a condition for that parameter. I hope this help.
Your select statement does not have a where clause for tran_id, either remove the tran_id from your execute call
$sth->execute(array(':user_name_buyer ' => $_SESSION['user_name']));
or add a extra where clause to your sql statement
$sql = "SELECT tran_id, seller_user_name, user_name_buyer
FROM trade_transaction, feedback
WHERE feedback.feedback_username = trade.user_name_of_buyer
AND tran_id = :tran_id
AND user_name_of_buyer = :user_name_buyer ";