PHP& MySQL:使用mysqli-> prepare()时,zerofill丢失 [英] PHP & MySQL: zerofill is lost when using mysqli->prepare()

查看:87
本文介绍了PHP& MySQL:使用mysqli-> prepare()时,zerofill丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用: MySQL 5.1 + PHP 5.3.5

Using: MySQL 5.1 + PHP 5.3.5

MySQL:

id in "table" is defined as: mediumint(6) zerofill not null

使用时,我得到了预期的结果:

I get the expected result when using:

$mysqli->query("SELECT id FROM table WHERE id = 1");
while($row = $ret->fetch_array(MYSQLI_ASSOC)) $arr[] = $row;
>>> $arr[0]["id"] = 000001

但是当我使用准备好的语句时不会:

But not when I use prepared statement:

$ret = $mysqli->prepare("SELECT id FROM table WHERE id = ?");
call_user_func_array(array($ret,"bind_param"),array("i",1));
$ret->execute();
$ret->store_result();
$meta = $ret->result_metadata();
$fields = $meta->fetch_fields();
$cols = array(); $data = array();
foreach($fields as $field) $cols[] = &$data[$field->name];
call_user_func_array(array($ret, 'bind_result'), $cols);
while($ret->fetch()) {
    $row = array();
    foreach($data as $key => $val) $row[$key] = $val;
    $arr[] = $row;
}
>>> $arr[0]["id"] = 1

直接在MySQL控制台中尝试准备好的语句时,它显示为预期的样子(不是MySQL).

When trying the prepared statement directly in the MySQL console, it shows as expected (it is not MySQL).

根据PHP mysqli_stmt :: bind_result 文档我发现了:

According to PHP mysqli_stmt::bind_result documentation I found this:

根据列类型,绑定变量可以无提示地更改为 相应的PHP类型.

Depending on column types bound variables can silently change to the corresponding PHP type.

我需要显示带有尾随零的数字,而无需在后续步骤中进行.填零的字段太多了,从数据到屏幕的流程实际上是自动化的,因此在此代码之后尝试解决此问题,将需要市长更改.

I need to show the number with the trailing zeros WITHOUT having to do it in a later step. There are so many fields with zerofill and the process is practically automated from the data to the screen, so trying to fix this after this code, will require mayor changes.

欢迎提供有关如何解决此问题的任何提示.

Any hint of how to solve this problem is welcome.

推荐答案

另一种解决方案是将字段指定为十进制(6,0).由于某种原因,它可以正常工作(无需更改代码).

Another solution was to specify the field as decimal(6,0). For some reason it works as expected (no changes to the code required).

这篇关于PHP& MySQL:使用mysqli-> prepare()时,zerofill丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆