如何使用PDO Prepared Statement在MySQL中插入BIT值?以下是我的尝试和结果。
<?php
function testIt($value)
{
$sql='INSERT INTO test(id,data) VALUES(?,?)';
$stmt=db::db()->prepare($sql);
$stmt->execute(array(0,$value));
$id=db::db()->lastInsertId();
$sql='SELECT * FROM test WHERE id='.$id;
$stmt=db::db()->query($sql);
$rs=$stmt->fetch(PDO::FETCH_ASSOC);
echo("Test for {$value} returns id {$rs['id']} and data {$rs['data']}<br>");
}
date_default_timezone_set('America/Los_Angeles');
ini_set('display_errors', 1);
error_reporting(E_ALL);
require_once('../../ayb_private/dbase.php');
$sql='CREATE TEMPORARY TABLE test (id INT UNSIGNED NOT NULL AUTO_INCREMENT, data BIT(8) NOT NULL DEFAULT 00000000, PRIMARY KEY (id) )';
$stmt=db::db()->exec($sql);
testIt('b"01010101"');
testIt('b01010101');
testIt('01010101');
testIt(0x55);
testIt("b'01010101'");
?>
结果:
Test for b"01010101" returns id 1 and data 255
Test for b01010101 returns id 2 and data 255
Test for 01010101 returns id 3 and data 255
Test for 85 returns id 4 and data 255
Test for b'01010101' returns id 5 and data 255
答案 0 :(得分:5)
不靠近要检查的终端,但我相信你必须输入绑定到INT并将其作为INT发送,而不是&#34; b010101&#34; (或其他):
$sql='INSERT INTO test(id,data) VALUES(:id,:bit)';
$stmt=db::db()->prepare($sql);
$stmt->bindValue('id', null, PDO::PARAM_NULL);
$stmt->bindValue('bit', (int)$value, PDO::PARAM_INT);
$stmt->execute();
Google快速检查了similar previous answer。