How do I make sure that values from MySQL keep their type in PHP?

眉间皱痕 提交于 2019-12-05 08:24:13

What exactly do I do to get my MySQL functions in PHP to give me the MySQL results in their native type?

You connect to the database, then you prepare your query, execute it, bind the result and then you fetch it.

Let's do these steps line-by-line:

$conn = new Mysqli('localhost', 'testuser', 'test', 'test');
$stmt = $conn->prepare("SELECT id FROM config LIMIT 1");
$stmt->execute();
$stmt->bind_result($id);
$stmt->fetch();
var_dump($id); # it's an int!

This works for me. As you wrote your code is more complex, you will need to locate the place where you query the database. Check that you're using Mysqli::prepare() and if not, introduce it.

You will also need to use Mysqli_Stmt::execute() and then Mysqli_Stmt::bind_result() otherwise the (here integer) type is not preserved for that result column.

However, my values are still being returned as strings.

You are in php man, in php it does not matter that your datas are int, bool, strings... if they have such a type they are called scalar datas and dynamaic castings will allow you to make them behave as you want. For example the string "12345"+"54321" will give you 66666. If you absolutely want your datas to be of a particular type, as in every language, it is not the driver's job. In java you've got something like .getString, .getInt methods in JDBC's interfaces, in php you do not have as it is not very usefull. You wil have to cast yourself your datas with intval boolval strval... functions or (int) ,(bool)... casting operators.

As your post said you can have it by using server-side prepared statement :

Advantages of using mysqlnd for PDO

mysqlnd returns native data types when using Server-side Prepared Statements, for example an INT column is returned as an integer variable not as a string. That means fewer data conversions internally.

With PDO

You have to put this line after your connection

$PDO->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);

then, when you want to query :

$s = $PDO->prepare('yourquery');
//here, binding params
$s->bindValue('paramName','paramValue');
$s->execute();

With Mysqli

As you use mysqli, the synthax will be a little different : Nota : there is no way of client side prepared statement so you won't need the configuration line that I put with PDO.

So your query will look like that :

$statement = $MySQli->prepare('your query');
$statement->bindParam('si',$stringParam,$intParam);
$statement->bindResult($var1,$var2/*,...*/);
$statement->execute();
while($statement->fetch()){
  //process here, result will be in var1, var2...
}

You can see that, here, there is not built-in fetchAll method. To bind your datas you need to use variables as it is not passed as value like in PDOStatement::bindValue() but by reference. Moreover the types are defined in the first arg (s for string, i for integer...) There is no named parameters only indexed ones. The fetch method works in a different way and needs you to call bindResult BEFORE the execute statement;

This probably is not the best answer but you can use something such as intval() to reinforce the data type. I've personally had this experience when using PDO, Mysqli, or even the old (now deprecated) mysql functions. You can run test cases with is_int() and is_string() to be 100% certain that the fetched data from the database really is a string versus and int. Sorry I couldn't be more help!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!