...

View Full Version : bind_param in a for loop



questionable
04-13-2009, 10: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)
$data->bind_param('s',$variable[0])
if(count($varable ==2)
$data->bind_param('ss',$variable[0],$variable[1])
etc...

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

Thanks!

bdl
04-14-2009, 01: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).

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



global $datab //mydatabase connection;
$fields = $datab->query("DESCRIBE myTable");
$count='0';
$values = array();
while ($result = $fields->fetch_array()) //build list of all the columns in the table
{
$fieldNames[$count] = $result[0];
$count++;
}
$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
$addToTable->prepare($sql);
$args = array_merge(array($types),$values);
call_user_func_array(array($addTable,'bind_param'),$args); //bind my parametes (replaces $addTable->bind_param('sss',...
$addToTable->execute();
$addToTable->close();

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum