Jump to content

MySQLi : Dynamic bind_param() parameters


Go to solution Solved by mac_gyver,

Recommended Posts

I would like to dynamically pass data for bind parameters, depending on user selection.
This code gives me two errors:

Warning: Invalid argument supplied for foreach()
Warning: Wrong parameter count for mysqli_stmt::bind_param()
$query="SELECT COUNT(*) FROM database WHERE parameter_zero ='approved'";

if(!empty($_GET['first_parameter'])) {

$queryPart[] = 'first_parameter=?';
$type[]='s';
$value[]=$_GET['first_parameter'];

}


if(!empty($_GET['second_parameter'])) {

$queryPart[] = 'second_parameter=?';
$type[]='s';
$value[]=$_GET['second_parameter'];

}


if(!empty($_GET['third_parameter'])) {

$queryPart[] = 'third_parameter=?';
$type[]='s';
$value[]=$_GET['third_parameter'];

}

$query.= ' AND '.implode(' AND ',$queryPart);
$bind=array_merge($type,$value);

if ($res = $con->prepare($query)) {


call_user_func_array(array($count,'bind_param'),ref($bind));
$res->execute();
$cRes = $res-> $fetch_row();
$count -> close();

} else error($con->error);

echo $cRes;

function ref($arr) {
    
    $refs=array();
    foreach($arr as $key => $value) {
        $refs[$key] = &$arr[$key];
    }
    return $refs;
}
Edited by undertaker
Link to comment
https://forums.phpfreaks.com/topic/281071-mysqli-dynamic-bind_param-parameters/
Share on other sites

  • Solution
$query="SELECT COUNT(*) FROM `database` WHERE parameter_zero ='approved'";

if(!empty($_GET['first_parameter'])) {
    $queryPart[] = 'first_parameter=?';
    $type[]='s';
    $value[]=$_GET['first_parameter'];
}

if(!empty($_GET['second_parameter'])) {
    $queryPart[] = 'second_parameter=?';
    $type[]='s';
    $value[]=$_GET['second_parameter'];
}

if(!empty($_GET['third_parameter'])) {
    $queryPart[] = 'third_parameter=?';
    $type[]='s';
    $value[]=$_GET['third_parameter'];
}

$query.= ' AND '.implode(' AND ',$queryPart);

if ($res = $con->prepare($query)) {

    $types = array(implode('',$type));
    $refs = ref($value);
    $bind=array_merge($types,$refs);
    
    call_user_func_array(array($res,'bind_param'),$bind);
    $res->execute();
    $Res = $res->get_result(); // if using mysqlnd driver, else you must use bind_result()
    list($cRes) = $Res->fetch_row();
    $res->close();

} else error($con->error);

echo $cRes;

function ref(&$arr){ // added & in call time parameter definition, must be a reference to the actual data
    
    $refs=array();
    foreach($arr as $key => $value) {
        $refs[$key] = &$arr[$key];
    }
    return $refs;
}

it looks like you just copy/pasted together something. at the level of trying to dynamically produce a prepared query, you really need to know what each statement does.

 

you also need to test your code when there are no $_GET parameters to make sure your query statement is correctly being built.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.