Using one parameter multiple times in prepared mysqli-statement

梦想与她 提交于 2019-12-23 13:43:04

问题


Is it possible to use one parameter in a prepared mysqli-statement multiple times with only binding it one time?
something like this

$stmt = $mysqli->prepare(SELECT * FROM user WHERE age BETWEEN ?1 - 2 AND ?1 + 2);
$stmt->bind_param('i', $myAge);

I think this is possible with PDO, but I don't konw how to do this with mysqli.


回答1:


Just to close the question:

The answer is no.

If you want to bind a parameter only one time and using it multiple times in a query you have to use PDO and this maybe also needs a special configuration.

But there seems to be more reasons to use PDO instead of mysqli, according to this great answer or this.

But sure there are workarounds. See the other answers to this question.




回答2:


You can use this instead

$stmt = $mysqli->prepare(SELECT * FROM user WHERE age BETWEEN ? - 2 AND ? + 2);
$stmt->bind_param('ii', $my_age, $my_age);



回答3:


You can use execute without bind_param. I am using array_fill to do the trick.

$stmt->execute(array_fill(0, 1, $myage));

You can edit the array_fill values depending on the statement, Suppose you have 3 values to fill, you can use:

$stmt->execute(array_fill(0, 2, $myage));


来源:https://stackoverflow.com/questions/22736358/using-one-parameter-multiple-times-in-prepared-mysqli-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!