2

I've scanned through the PHP documentation regarding the MySQLi class, and have read it's possible but cannot seem to find what option to set to prevent Prepared Statements from returning native data types for results, rather than just strings.

From Example #5 on this php documentation page, it says

By default, non-prepared statements return all results as strings. This default can be changed using a connection option. If the connection option is used, there are no differences.

But what's the connection option and how do I go about setting it?

Currently my non prepared statements and prepared statements are outputting data like this..

Non Prepared (Normal Query):

"Foo" => (string)"1"

Prepared:

"Foo" => (int)1

I want them both to return strings for output.

Braydon Batungbacal
  • 1,028
  • 2
  • 24
  • 52
  • Good question but why does it matter? Also, I think changing the *default* (as mentioned in the documentation) will only change it for non-prepared statements anyway – Phil Jul 17 '14 at 05:55
  • I have a very large application that makes use of both regular and prepared queries. Regular queries for retrieving data where user input is not a factor to prevent the additional overhead of a prepared statement, and prepared statements where user input is necessary. The reliant mobile side is dependent on all values sent down from the server to be json encoded strings. With prepared statements, these values are not in the expected format. – Braydon Batungbacal Jul 17 '14 at 05:58
  • Ah, so the real problem is that numbers in your JSON need to be strings, right? Again, that seems an odd requirement but in any case, see [this answer](http://stackoverflow.com/a/6608413/283366) – Phil Jul 17 '14 at 06:02
  • The application was originally built on MySQL not MySQLi, so outputs were expected as strings since MySQL PHP library returns all strings. Converting the application to make use of the data types wouldn't be difficult. Regardless of that, the problem is still that the 2 different query types send data in 2 different formats. Non prepared statements being all strings, prepared statements being whatever the data type is. It needs to consistent between both, meaning I need an answer haha :) – Braydon Batungbacal Jul 17 '14 at 06:04
  • Choosing between prepared and non-prepared queries is really micro-optimisation. Have you actually observed a noticeable performance difference? – Phil Jul 17 '14 at 06:08
  • I have not. However the use of prepared statements for queries without any bound parameters seems strange? Is there a common practice here that I'm missing? Are prepared statements typically used for queries with no inputs as well? – Braydon Batungbacal Jul 17 '14 at 06:16
  • 1
    Also, this is irritating. MySQLi returns all String datatypes EXCEPT for int... which returns int. Decimal and other values are returned as strings which is strange and inconsistent in my opinion. – Braydon Batungbacal Jul 17 '14 at 06:43
  • I strongly suspect this should be reported to the PHP team as documentation problem: either the feature exists (but it's clearly not documented) or doesn't exist (thus should be removed from docs). – Álvaro González Jul 17 '14 at 09:52

0 Answers0