note 71987 modified in function.mysqli-stmt-store-result by danbrown
| From: | danbrown@php.net | Date: | Fri, 28 Nov 2008 22:17:09 +0000 |
| Subject: | note 71987 modified in function.mysqli-stmt-store-result by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-147317@lists.php.net to get a copy of this message | ||
In response to the note below me for the claim that mysqli_fetch_fields is not compatible with
prepared statements.
This is untrue, it is but you have to do a little extra work. I would recommend you use a wrapper
function of some sort to take care of the dirty business for you but the basic idea is the same.
Let's assume you have a prepared statement like so. I am going to use the procedural way for
simplicity but the same idea can be done using the object oriented way:
<?php
// Connect Blah Blah Blah.
$connectionLink = mysqli_connect( .... );
// Query Blab Blah Blah.
$query = "Select
Id From Table Where Id = ?";
// Prepare Query.
$prepareObject = mysqli_prepare( $connectionLink , $query );
// Bind Query.
mysqli_stmt_bind_param( $prepareObject , 'i' , 1 );
// Execute Query.
mysqli_stmt_execute( $prepareObject );
?>
Now all the above is fine and dandy to anyone familiar with using prepared statements, but if I want
to use mysqli_fetch_fields or any other function that fetches meta information about a result set
but does not work on prepared statements?
Enter the special function mysqli_stmt_result_metadata. It can be used as follows, assume the
following code segment immediatley follows that of the above code segment.
<?php
$metaData = mysqli_stmt_result_metadata( $prepareObject );
// I Can Now Call mysqli_fetch_fields using the variable
// $metaData as an argument.
$fieldInfo = mysqli_fetch_fields( $metaData );
// Or Even This.
$fieldInfo = mysqli_num_fields( $metaData );
?>
Take a look at the Manual entry for mysqli_stmt_result_metatdata function for full details on how to
expose it with prepared statements.
Good Luck,
--was--
In response to the note below me for the claim that mysqli_fetch_fields is not compatible with
prepared statements.
This is untrue, it is but you have to do a little extra work. I would recommend you use a wrapper
function of some sort to take care of the dirty business for you but the basic idea is the same.
Let's assume you have a prepared statement like so. I am going to use the procedural way for
simplicity but the same idea can be done using the object oriented way:
<?php
// Connect Blah Blah Blah.
$connectionLink = mysqli_connect( .... );
// Query Blab Blah Blah.
$query = "Select Id From Table Where Id = ?";
// Prepare Query.
$prepareObject = mysqli_prepare( $connectionLink , $query );
// Bind Query.
mysqli_stmt_bind_param( $prepareObject , 'i' , 1 );
// Execute Query.
mysqli_stmt_execute( $prepareObject );
?>
Now all the above is fine and dandy to anyone familiar with using prepared statements, but if I want
to use mysqli_fetch_fields or any other function that fetches meta information about a result set
but does not work on prepared statements?
Enter the special function mysqli_stmt_result_metadata. It can be used as follows, assume the
following code segment immediatley follows that of the above code segment.
<?php
$metaData = mysqli_stmt_result_metadata( $prepareObject );
// I Can Now Call mysqli_fetch_fields using the variable
// $metaData as an argument.
$fieldInfo = mysqli_fetch_fields( $metaData );
// Or Even This.
$fieldInfo = mysqli_num_fields( $metaData );
?>
Take a look at the Manual entry for mysqli_stmt_result_metatdata function for full details on how to
expose it with prepared statements.
Good Luck,
http://php.net/manual/en/function.mysqli-stmt-store-result.php