1

short question, but apparently google nor php.net have no idea about this so I was curious if it is possible.

Can you get the query string of an mysqli element? I need it to make a proper error function

$mysqli = new mysqli(....);
$mysqli->query('INSERT INTO test(value) VALUES ('.rand(1,10000).');');
// here how do i find out the query of the $mysqli object WITHOUT having 
// to save it before (i.e. using the actual $mysqli object)

Thanks!

Stefan
  • 3,962
  • 4
  • 34
  • 39
  • Possible duplicate of http://stackoverflow.com/questions/962986/how-to-echo-a-mysqli-prepared-statement + http://stackoverflow.com/questions/2691707/is-there-any-way-to-print-the-actual-query-that-mysqli-execute-makes – newfurniturey Aug 09 '12 at 21:22
  • 1
    I personally do not use the mysqli methods directly, but write my own wrapper class. The query method there will check for an error and if there is one, it still has the SQL query in the local variable anyway. The benefits of doing it this way are 1) abstract DB so I could change vendors, 2) better error handling 3) automatically handle escaping 4) convenience methods like $db->getRow("...") and $db->getRows and $db->getValue etc. – webjprgm Aug 09 '12 at 21:23
  • Your question is hard to understand, it would be good to rewrite it and add an example of what you expect exactly. – Jocelyn Aug 09 '12 at 21:31

2 Answers2

1

Judging from http://www.php.net/manual/en/class.mysqli-result.php - you can't.

$mysqli->query() is a method call, which returns mysqli_result object, and looking at the above manual page, it contains no record of the query that was just executed to create it.

favoretti
  • 29,299
  • 4
  • 48
  • 61
-2

Not sure I understand your question, but:

$sql = "INSERT INTO test(value) VALUES ('.rand(1,10000).');";
$mysqli->query($sql);

...then, later in your scirpt, you can just reference $sql:

if($sql = '...'){...}

Etc.

Madbreaks
  • 19,094
  • 7
  • 58
  • 72
  • 2
    Well, It wasn't me, but based on the question you're referring back to the built query from a string. The OP wanted a string returned from the mysqli object. Not the one he built. It's so that was you can make sure the query was ran. – Twister1002 Jul 24 '14 at 15:11