How to echo a MySQLi prepared statement?
I don't think you can - at least not in the way that you were hoping for. You would either have to build the query string yourself and execute it (ie without using a statement), or seek out or create a wrapper that supports that functionality. The one I use is Zend_Db, and this is how I would do it:
$id = 5;
$baz = 'shazam';
$select = $db->select()->from('bar','foo')
->where('id = ?', $id)
->where('baz = ?', $baz); // Zend_Db_Select will properly quote stuff for you
print_r($select->__toString()); // prints SELECT `bar`.`foo` FROM `bar` WHERE (id = 5) AND (baz = 'shazam')
How do I display/echo the data from an executed mysqli prepared statement
edit:
add those rules after the execute.
$res = $query->get_result();
$row = $res->fetch_assoc();
and after that you can do what ur used to do with it, with the old way.
How to display results from mysqli prepared statement?
You have a couple of issues. The first is that you are binding 6 results, but your query only returns 4. The second is that mysqli_stmt::fetch
simply returns a boolean indicating success/fail since all result data is fetched into the bound result parameters. For your query as is, you need to do something like:
$query->bind_result($first_name, $last_name, $phone, $email);
while ($query->fetch()) {
echo $first_name, $last_name, $phone, $email;
}
Notes:
- Since you are using a parameterised query, you don't need to use
escape_string
on your inputs. - It's more efficient to add the
%
to either side of the parameter once in PHP (i.e.$keywords = "%$keywords%";
) and just useLIKE ?
rather than 5 instances ofLIKE CONCAT('%',?,'%')
in your query.
Converting to Prepared Statements - How to echo results of the query
You have to think of storing columns one by one like this:
$stmt->execute();
$stmt->bind_result($col1, $col2);
or use code from this answer, which gives you an array of all columns without specyfing each other.
php mysqli prepared statement: can't echo, print or get anything from var_dump AFTER execute command
Ok problem solved. My real server didn't support mysqlnd so can't use get_result(). Thanks for the help guys!
Related Topics
Using PHP/Apache to Restrict Access to Static Files (Html, CSS, Img, etc)
Installing Specific Laravel Version with Composer Create-Project
Str_Replace for Multiple Items
Not Equal to != and !== in PHP
Check Whether or Not a Cidr Subnet Contains an Ip Address
Can't Install Laravel Installer via Composer
Crop Whitespace from Image in PHP
Get Variable from PHP to JavaScript
How to Dynamically Write a PHP Object Property Name
How to Enable Memcache in Wamp
Dynamic Class Method Invocation in PHP
Getting ’ Instead of an Apostrophe(') in PHP
Header in PDF Page Using Dompdf in PHP