note 108790 added to mysqli-stmt.bind-param
| From: | robstocki at battlesecure dot com | Date: | Thu, 24 May 2012 22:22:40 +0000 |
| Subject: | note 108790 added to mysqli-stmt.bind-param | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-188211@lists.php.net to get a copy of this message | ||
Here is the procedural version of a select statement when wanting to use %LIKE% in the query and not
an '=':
<?php
function db_connect(){
// set $db as global for access outside function
global $db;
# Use procedural methods for database connection and manipulation
// Connect to Database
@$db = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// Check connection
if(mysqli_connect_errno()) {
echo '<br />Error: Could not connect to database. Please try again later.<br
/>';
exit;
}
}
// Connect to Database
db_connect();
$theRecordInTheTableIs =
"%".$_POST['theRecordInTheTableIs']."%"; // concat $_POST variable
with % on each side for use in prepared statement
$theRelatedRecordLooks =
"%".$_POST['theRelatedRecordLooks']."%"; // concat $_POST variable
with % on each side for use in prepared statement
// Create Query
$q = ''; // create variable for input
$q .= " SELECT ";
$q .= " * ";
$q .= " FROM ";
$q .= " myFavTable ";
$q .= " WHERE ";
$q .= " theRecordInTheTableIs LIKE ? ";
$q .= " AND ";
$q .= " theRelatedRecordLooks LIKE ? ";
// Prepare the statement
$stmt = mysqli_prepare($db, $q);
// bind the paramaters
mysqli_stmt_bind_param($stmt, 'ss', $theRecordInTheTableIs,$theRelatedRecordLooks);
// Execute the query
mysqli_stmt_execute($stmt);
// bind result variables
mysqli_stmt_bind_result($stmt, $col0, $col1, $col2, $col3, $col4, $col5, $col6, $col7, $col8,
$col9, $col10);
// Display the query
// start the table
echo '<table>
<tr>
<td>Col0 Title</td>
<td>Col1 Title</td>
<td>Col2 Title</td>
<td>Col3 Title</td>
<td>Col4 Title</td>
<td>Col5 Title</td>
<td>Col6 Title</td>
<td>Col7 Title</td>
<td>Col8 Title</td>
<td>Col9 Title</td>
<td>Col10 Title</td>
</tr>';
// fetch values
while (mysqli_stmt_fetch($stmt)) {
echo " <tr>
<td>{$col0}</td>
<td>{$col1}</td>
<td>{$col2}</td>
<td>{$col3}</td>
<td>{$col4}</td>
<td>{$col5}</td>
<td>{$col6}</td>
<td>{$col7}</td>
<td>{$col8}</td>
<td>{$col9}</td>
<td>{$col10}</td>
</tr> ";
}
// close the table
echo '</table>';
?>
----
Server IP: 69.147.83.197
Probable Submitter: 216.106.110.3
----
Manual Page -- http://www.php.net/manual/en/mysqli-stmt.bind-param.php
Edit -- https://master.php.net/note/edit/108790
Del: integrated -- https://master.php.net/note/delete/108790/integrated
Del: useless -- https://master.php.net/note/delete/108790/useless
Del: bad code -- https://master.php.net/note/delete/108790/bad+code
Del: spam -- https://master.php.net/note/delete/108790/spam
Del: non-english -- https://master.php.net/note/delete/108790/non-english
Del: in docs -- https://master.php.net/note/delete/108790/in+docs
Del: other reasons-- https://master.php.net/note/delete/108790
Reject -- https://master.php.net/note/reject/108790
Search -- https://master.php.net/manage/user-notes.php