2

Is there any function that will return the prepared query string after processing all the parameters. like

$stmt = $conn->prepare("SELECT full_name FROM user_info where user_id = ?");
$stmt->bind_param("s", $user_id);

Can I see the final query string that will execute?

Shakir Blouch
  • 187
  • 1
  • 5
  • 14
  • you can use the function in this answer([https://stackoverflow.com/questions/45031956/switching-to-prepared-statements/45034865#45034865](https://stackoverflow.com/questions/45031956/switching-to-prepared-statements/45034865#45034865)) – Accountant م Jul 25 '17 at 22:28
  • sorry I misunderstood the question. I thought you need a function that wrap the prepared statements process, (prepare, bind, execute) and return the result object – Accountant م Jul 25 '17 at 22:47
  • I need to show/get the exact query(string) without execution. – Shakir Blouch Jul 25 '17 at 22:50
  • I understand now what you want, you need to see `SELECT full_name FROM user_info where user_id = 6` instead of `SELECT full_name FROM user_info where user_id = ?` . Try to look at the duplicate question. Also note that tadman said in the answer that the *final query* is the prepared statement *with question marks `?`* If you really need this , you can replace the question marks with the variables with PHP. – Accountant م Jul 25 '17 at 22:57

1 Answers1

1

If the driver is capable of using prepared statements, if it doesn't require emulation, then the final query executed is the prepared statement.

If you want to find out what was executed, you need to turn on the general query log on your server. That can be very, very noisy and fill up your disk quickly on a busy server.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • I need to get the query without execution – Shakir Blouch Jul 25 '17 at 22:31
  • Hey, thanks for your advise about prepared statements [here](https://stackoverflow.com/questions/37304917/how-to-optimize-this-sql-insertion-php-code#comment62131691_37304917) I remember you when I was on the start line of that programming road - I'm still on the start line though :D - Now I'm using prepared statements exclusively as you told me. – Accountant م Jul 25 '17 at 22:38
  • 1
    @Accountantم Thanks for letting me know! Sometimes this constant badgering can seem futile, but it's stories like yours that help make it relevant and important. I think you'll sleep a lot easier knowing that your code's written correctly and won't be a liability no matter how widely it's deployed, plus can serve as a useful example for others. – tadman Jul 25 '17 at 23:29