Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jun 2005
    Posts
    153
    Thanks
    26
    Thanked 0 Times in 0 Posts

    bind_param in a for loop

    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
    Code:
    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!
    Last edited by questionable; 04-14-2009 at 07:09 PM. Reason: Should be PHP

  • #2
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    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 : mysqli_stmt::bind_param.

  • #3
    Regular Coder
    Join Date
    Jun 2005
    Posts
    153
    Thanks
    26
    Thanked 0 Times in 0 Posts

    For those who are interested here's the solution

    $datab is my database object connection

    Code:
    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!
    Last edited by questionable; 04-14-2009 at 07:09 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •