3

I have the following code

$name = 'myname';
$mail = 'my@mail.it';
$qwt = "INSERT INTO `agenz` (`name`, `email`) VALUES (?,?)";
$result = $connessione->prepare($qwt);
$result->bind_param('ss', $name, $mail); 
$result->execute(); 

I want print the query

"INSERT INTO `agenz` (`name`, `email`) VALUES ('myname','my@mail.it')"

for creating a log.So how to do that?

thanx.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • 1
    Possible duplicate of [How can I get the SQL of a PreparedStatement?](https://stackoverflow.com/questions/2382532/how-can-i-get-the-sql-of-a-preparedstatement) – CBroe Jun 07 '17 at 10:19
  • May be [this](http://www.dummies.com/programming/php/how-to-display-php-variable-values/) will be helpfull – Sharad Gautam Jun 07 '17 at 10:22
  • ***If it's `mysqli` then you can't do it***. why? check below links:-[How can I get the SQL of a PreparedStatement?](https://stackoverflow.com/a/2382561/4248328)AND [Is there any way to print the actual query that mysqli->execute() makes?](https://stackoverflow.com/questions/2691707/is-there-any-way-to-print-the-actual-query-that-mysqli-execute-makes)AND[How to echo a MySQLi prepared statement?](https://stackoverflow.com/questions/962986/how-to-echo-a-mysqli-prepared-statement) – Alive to die - Anant Jun 07 '17 at 14:49

3 Answers3

2

Try fullQuery like below:

$name = 'myname';
$mail = 'my@mail.it';
$qwt = "INSERT INTO `agenz` (`name`, `email`) VALUES (?,?)";
$result = $connessione->prepare($qwt);
$result->bind_param('ss', $name, $mail); 
$result->execute(); 

echo $result->fullQuery;

or

$result->debugQuery();
var_dump($result->debugBindedVariables());

if you want to get the error

var_dump($result->errno);
lalithkumar
  • 3,480
  • 4
  • 24
  • 40
1

I have solved!

  $qwt = "INSERT INTO `age` (`name`,`email`) VALUES (?,?)";
  $par = array($name, $mail);

  $result = $connessione->prepare($qwt);
  $result->bind_param('ss', $par[0], $par[1]); 
  $result->execute();

  echo "contenuto: " .SqlDebug($qwt, $par); 

  function SqlDebug($raw_sql, $params=array())
  {
  $keys = array();
  $values = $params;  
  foreach ($params as $key => $value)
  {
    // check if named parameters (':param') or anonymous parameters ('?') are    used
    if (is_string($key)) { $keys[] = '/:'.$key.'/'; }
    else                 { $keys[] = '/[?]/'; }
    // bring parameter into human-readable format
    if (is_string($value))    { $values[$key] = "'" . $value . "'"; }
    elseif (is_array($value)) { $values[$key] = implode(',', $value); }
    elseif (is_null($value))  { $values[$key] = 'NULL'; }
  }
  $raw_sql = preg_replace($keys, $values, $raw_sql, 1, $count);
  return $raw_sql;
  }
0

You can use variable directly in the statement like following

$name="ABC";
$mail="me@someone.com";
$value= "I am $name mail id $mail";
Sharad Gautam
  • 91
  • 5
  • 11