I have DB class which is dealing all queries will be made to database I have mysqli prepare working fine. bind_param is also working fine but the problem is I want to define variable type dynamically. here is my code
public function query($sql, $params = array()){
$this->_error = false;
if($this->_query = $this->_mysqli->prepare($sql)){
$x = 1;
if(count($params)){
foreach($params as $param){
$this->_query->bind_param($x, $param);
$x++;
}
}
IN PDO fist parameter defines position I guess so this function runs fine by setting X = 1 and x++ everytime,
but in bind_param
first argument defines type I guess
as php.net manual says
so is there is any way if user pushes integral value I set
x = i
for string
x = s
so on and so forth for all 4 types ...
like
if((int)$param->){
x = i;
}
any Idea guys?
thanks in advance
For types it's easy. Just use
s
all the way around.There is a much more complex problem: in fact, you cannot bind in a loop, so, have to use
call_user_func()
Note that you shouldn't assign a statement to a local variable and there is no use for the error variable as well. Exceptions are better in every way.
Looking at the code above you should think twice before turning over PDO, which will take only three lines for such a function:
If you have no experience with PDO, here is a PDO tutorial I wrote, from which you will learn that it's most simple yet powerful database API, getting you data in dozens different formats, with very little amount of code.
Here is an example that could help (
prepare()
function is a class method ).Example of usage: