Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用mysqli时会丢失Zerofill->;准备()_Php_Mysql_Mysqli_Prepared Statement_Zerofill - Fatal编程技术网

Php 使用mysqli时会丢失Zerofill->;准备()

Php 使用mysqli时会丢失Zerofill->;准备(),php,mysql,mysqli,prepared-statement,zerofill,Php,Mysql,Mysqli,Prepared Statement,Zerofill,使用: MySQL 5.1+PHP 5.3.5 MySQL: id in "table" is defined as: mediumint(6) zerofill not null 我在使用时得到了预期的结果: $mysqli->query("SELECT id FROM table WHERE id = 1"); while($row = $ret->fetch_array(MYSQLI_ASSOC)) $arr[] = $row; >

使用: MySQL 5.1+PHP 5.3.5

MySQL:

id in "table" is defined as: mediumint(6) zerofill not null
我在使用时得到了预期的结果:

$mysqli->query("SELECT id FROM table WHERE id = 1");
while($row = $ret->fetch_array(MYSQLI_ASSOC)) $arr[] = $row;
>>> $arr[0]["id"] = 000001
但当我使用事先准备好的声明时:

$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控制台中尝试prepared语句时,它会如预期的那样显示(它不是MySQL)

根据PHP文档,我发现:

根据列类型,绑定变量可以静默地更改为 对应的PHP类型


我需要显示带有尾随零的数字,而不必在后面的步骤中进行显示。zerofill有很多字段,而且从数据到屏幕的过程实际上是自动化的,因此在这段代码之后尝试修复这一点需要进行重大更改。

暂时,我通过添加以下行解决了这一问题:

define("ZEROFILL_FLAG",64);
$zeros = array();
foreach($fields as $field) {
    $zeros[$field->name] = $field->flags & ZEROFILL_FLAG ? $field->length : 0;
    $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] = $zeros[$key] > 0 ? sprintf('%0'.$zeros[$key].'s', $val) : $val;
    }   
    $arr[] = $row;
}  

我仍然希望有人能给我一个更好的方法来解决这个问题。

另一个解决方案是将字段指定为十进制(6,0)。由于某种原因,它可以按预期工作(不需要更改代码)。

零填充丢失,因为mysqli知道列的类型为integer,因此php变量的类型也变为int,因此前导零丢失

这似乎只有在mysqli中使用准备好的语句时才会发生(PDO似乎没有这种行为)

要防止mysqli将变量的类型更改为整数,必须将MySQL中的类型更改为以字符串形式返回的类型

有两种方法:

  • CAST(intCol作为CHAR)
    将包含前导零的整数强制转换为字符串
  • 在字符串上下文中使用零填充整数,例如
    CONCAT(intCol,“”)
  • 注意:如果在HTML5输入字段中使用该值,类型为
    number
    ,这也会去掉前导零(至少在某些浏览器中)。要解决这个问题,请看一看