Re: Prepared Statements - Select - Bind Parameters w/ correction

Matt Pelmear <[email protected]>
Newsgroups gmane.comp.php.database
Message-ID <[email protected]>
     $stmt = mysqli_stmt_prepare( $cxn, $sql12 );// line 507

//Warning: mysqli_stmt_prepare() expects parameter 1 to be mysqli_stmt, object given in /var/www/x5.php on line 507



$cxn is not a mysqli_stmt. Are you perhaps passing the mysqli database 
resource instead of the statement?

See this section of my example:

     $stmt = mysqli_prepare( $dbh, $q );
     if( !$stmt )
         throw new Exception( 'Error preparing statement' );
where $dbh is the result of mysqli_connect() and $q is a string 
containing your unbound query.

-Matt

On 09/28/2012 09:46 AM, Ethan Rosenberg, PhD wrote:
>
> Matt -
>
> Thanks.
>
> Here is what I used, and it still generates an error:
>
>         $allowed_fields = array
>             (  $_POST['Site'] => 's',  $_POST['MedRec']  => 'i', 
> $_POST['Fname'] => 's', $_POST['Lname'] => 's',
>              $_POST['Phone'] => 's',   $_POST['Height'] => 'i',   
> $_POST['Sex'] => 's',   $_POST['Hx'] => 's',
>              $_POST['Bday'] => 's',  $_POST['Age'] => 'i' );
>
>         if(empty($allowed_fields))
>         {
>              echo "ouch";
>         }
>
>    // Magically put everything together
>     $types = '';
>     $args = array();
>     foreach( $allowed_fields as $k => $type )
>     {
>         if( !array_key_exists( $k, $_POST ) )
>             continue;
>
>         $args[] = &$_POST[$k]; // Note the addition of the ampersand here
>         $types .= $type;
>         $sql12 .= " AND ($k = ?)";
>     }
> echo "new query $sql12";
>     // For debugging and demonstration
>     echo 'Query: ' . $sql12 . PHP_EOL;
>     echo 'Bind types: ' . $types . PHP_EOL;
>     echo 'Arguments:' . PHP_EOL;
>     print_r($args);
>
>     $stmt = mysqli_stmt_prepare( $cxn, $sql12 );// line 507
> //Warning: mysqli_stmt_prepare() expects parameter 1 to be mysqli_stmt, object given in /var/www/x5.php on line 507
>
>     if( !$stmt )
>         throw new Exception( 'Error preparing statement' ); // line 509
> //Fatal error: Uncaught exception 'Exception' with message 'Error preparing statement' in /var/www/x5.php on line 509
>
> //Exception: Error preparing statement in /var/www/x5.php on line 509
>
>
>     // Put the statement and types variables at the front of the 
> params to pass to mysqli_stmt_bind_param()
>     array_unshift( $args, $stmt, $types ); // Note that I've moved 
> this call. Apparently it doesn't pass back the result. I guess 
> sometimes I just forget these things.
>
>     // mysqli_stmt_bind_param()
>     if( !call_user_func_array( 'mysqli_stmt_bind_param', $args ) )
>         throw new Exception( 'Failed calling mysqli_stmt_bind_param' );
>
>     if( !mysqli_stmt_execute( $stmt ) )
>         throw new Exception( 'Error while executing statement' );
>     mysqli_stmt_bind_result( $stmt, $id, $data );
>
>     while( mysqli_stmt_fetch($stmt) )
>         printf( "%d %d\n", $id, $data );
>
>     mysqli_stmt_close( $stmt );
>  //   mysqli_close( $cxm );
>
>
> What did I do wrong???
>
> Ethan
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.