View Full Version : bind_param in a for loop

04-13-2009, 11:57 PM
Could this be moved to the PHP Threads?

Hey Guys,

I'm working with MYSQLI and I want to know if there is a way to bind_param with an unknown about of variables

So instead of

if(count($variable == 1)
if(count($varable ==2)

I would appreciate a way to put it into a for loop.


04-14-2009, 02:06 AM
Not a MySQL question.

Having an "unknown number of variables" sort of defeats the purpose, don't you think? What exactly are you trying to accomplish?

PHP Manual : MySQL Improved Extension (http://www.php.net/mysqli) : mysqli_stmt::bind_param (http://us2.php.net/manual/en/mysqli-stmt.bind-param.php).

04-14-2009, 07:03 PM
$datab is my database object connection

global $datab //mydatabase connection;
$fields = $datab->query("DESCRIBE myTable");
$values = array();
while ($result = $fields->fetch_array()) //build list of all the columns in the table
$fieldNames[$count] = $result[0];
$addToTable = $datab->stmt_init();
$str = '';
for($subcount = 0; $subcount < $count; $subcount++)
$types .= 's'; //build number of strings to insert
$str .= '?'; //number of variable in mysql query
if($subcount != ($count -1))
$str.= ','; //comma's between ? marks
$values[] = $_POST[$fieldNames[$subcount]];
$sql = "INSERT INTO myTable VALUES(".$str.")"; //prepare my wildcard statement
$args = array_merge(array($types),$values);
call_user_func_array(array($addTable,'bind_param'),$args); //bind my parametes (replaces $addTable->bind_param('sss',...

Sorry this really should be under the 'PHP' Section!