How to echo a MySQLi prepared statement

0 votes

In my current projects I always like to echo out a query string while coding, just to make sure that everything is correct, and to quickly debug my code. But how can I do this with a prepared MySQLi statement?

Example:

$id = 1;
$baz = 'something';

if ($stmt = $mysqli->prepare("SELECT foo FROM bar WHERE id=? AND baz=?")) {
  $stmt->bind_param('is',$id,$baz);
  // how to preview this prepared query before acutally executing it?
  // $stmt->execute();
}

Any help?

Nov 14, 2020 in PHP by kartik
• 37,520 points
3,379 views

1 answer to this question.

0 votes

Hello @kartik,

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 , or seek out or create a wrapper that supports that functionality. The one I use:

$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')

Hope it helps!!

answered Nov 14, 2020 by Niroj
• 82,840 points

Related Questions In PHP

0 votes
1 answer

How can to make a query with LIKE and get all results with mysqli ?

Hello @kartik, Here's how you properly fetch the ...READ MORE

answered Nov 17, 2020 in PHP by Niroj
• 82,840 points
829 views
0 votes
1 answer

What is a Cookie? How to create Cookies With PHP?

A cookie is often used to identify ...READ MORE

answered Feb 13, 2020 in PHP by Niroj
• 82,840 points
3,792 views
0 votes
1 answer

How to install a specific version of package using Composer?

Hello, Just use php composer.phar require For example : php ...READ MORE

answered Apr 1, 2020 in PHP by Niroj
• 82,840 points
3,627 views
0 votes
0 answers
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
23,012 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
2,995 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
2,918 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
44,697 views
0 votes
1 answer

How to include a PHP variable inside a MySQL statement?

Hii, You can try this: $query="SELECT * FROM CountryInfo ...READ MORE

answered Apr 9, 2020 in PHP by Niroj
• 82,840 points
16,995 views
0 votes
1 answer

How to add a line break within echo in PHP?

Hello @kartik, You have to use br when ...READ MORE

answered Oct 27, 2020 in PHP by Niroj
• 82,840 points
3,134 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP