我有一個MySQL表定義爲INT類型的列:mysqli的bind_result只工作了
CREATE TABLE IF NOT EXISTS `coupons` (
`coupon_id` int(11) NOT NULL auto_increment,
`coupon_code` varchar(50) NOT NULL,
`coupon_expiration_date` date NOT NULL,
`coupon_discount` decimal(10,2) NOT NULL,
`coupon_created_date` date NOT NULL,
PRIMARY KEY (`coupon_id`)
)
而在此表中的一個條目,如下所示:
INSERT INTO `coupons` (`coupon_id`, `coupon_code`, `coupon_expiration_date`, `coupon_discount`, `coupon_created_date`) VALUES (1, 'test', '2012-11-30', '11.00', '2012-10-22');
當試圖使用查詢該表以下PHP代碼:
$strSelectCoupons = "SELECT coupon_id FROM coupons";
if($stmtSelectCoupons = $mysqlConn->prepare($strSelectCoupons))
{
$stmtSelectCoupons->execute();
$stmtSelectCoupons->bind_result($iCouponId);
while($stmtSelectCoupons->fetch())
{
echo $iCouponId;
}
}
else
{
echo "Prepared statement error: " . $mysqlConn->error;
}
正如預期的那樣,我們看到在屏幕上打印出'1'。
但是,如果我們嘗試執行下面的PHP代碼:
$strSelectCoupons = "SELECT coupon_id, coupon_code FROM coupons";
if($stmtSelectCoupons = $mysqlConn->prepare($strSelectCoupons))
{
$stmtSelectCoupons->execute();
$stmtSelectCoupons->bind_result($iCouponId, $strCouponCode);
while($stmtSelectCoupons->fetch())
{
echo $iCouponId . "<br />";
echo $strCouponCode . "<br />";
}
}
else
{
echo "Prepared statement error: " . $mysqlConn->error;
}
未顯示結果,打印沒有錯誤,沒有錯誤被記錄並在頁面的其餘部分不渲染。
實際上,這個實際上按預期工作的唯一時間是隻返回coupon_id。例如,如果我們將select語句更改爲僅返回coupon_code,則這又會導致頁面被彈出。
任何想法是什麼導致這失敗?
Apache的版本:2.2.2
PHP版本:5.3.14
MySQL版本:5.0.95社區
編輯:
正在進一步調查中,bind_result是隻能處理INT類型的列,並且在嘗試綁定另一個類型的列時失敗。
你肯定'coupon_code'實際上是在表中的列名?也許在某個地方有一個錯字? – sgroves
只是想知道 - 當你執行工作查詢時,coupon_id auto_increment會實際增加值嗎? – Phil
sgroves:'coupon_code'是表中列的實際名稱。除了'coupon_id'外,所有列都會出現問題。 –