我有一个我以前从未遇到的奇怪问题。
我在名为description
的mysql表中有一个list_items
列。
在某些行中,描述为空,而某些行则不是。格式是中等文本,但是当我将其更改为VARCHAR(1000)之类的内容时会出现同样的问题。
这是一个简单的查询:
$result=mysql_query("select * from list_items where list_id='$id' order by position desc") or die(mysql_error());
while($iteminfo=mysql_fetch_assoc($result)){
print_r($iteminfo);
}
它给出了这样的东西:
Array (
[id] => 85 [list_id] => 16 [position] => 4
[item] => How To Be Richer Smarter And Better Looking Than Your Parents
[voted_up] => 0 [voted_down] => 0
[small_image] => http://ecx.images-amazon.com/images/I/51tdjl56TwL._SL160_.jpg
[large_image] => http://ecx.images-amazon.com/images/I/51tdjl56TwL.jpg
[asin] => 1591845440 [description] => undefined [author] => Zac Bissonnette
[publish_date] => 2012-04-24 [genre] => )
描述列始终是“未定义的”,无论它是否为空。有什么想法吗?
表格结构:
CREATE TABLE IF NOT EXISTS `list_items` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`list_id` int(11) NOT NULL,
`position` int(11) NOT NULL,
`item` varchar(512) NOT NULL,
`voted_up` int(11) NOT NULL,
`voted_down` int(11) NOT NULL,
`small_image` varchar(128) NOT NULL,
`large_image` varchar(128) NOT NULL,
`asin` varchar(16) NOT NULL,
`description` mediumtext NOT NULL,
`author` varchar(128) NOT NULL,
`publish_date` varchar(16) NOT NULL,
`genre` varchar(128) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=107 ;
var_dump返回:
array(13) { ["id"]=> string(2) "87" ["list_id"]=> string(2) "16" ["position"]=> string(1) "2" ["item"]=> string(22) "Game Of Thrones Book 1" ["voted_up"]=> string(1) "1" ["voted_down"]=> string(1) "0" ["small_image"]=> string(61) "http://ecx.images-amazon.com/images/I/51VcGBtiLTL._SL160_.jpg" ["large_image"]=> string(53) "http://ecx.images-amazon.com/images/I/51VcGBtiLTL.jpg" ["asin"]=> string(10) "0553386794" ["description"]=> string(0) "" ["author"]=> string(18) "George R.R. Martin" ["publish_date"]=> string(10) "2011-03-22" ["genre"]=> string(0) "" }
但echo $iteminfo['description']
仍然提供'未定义'。
答案 0 :(得分:1)
@robert,我发帖作为答案,所以你可以接受并关闭它。 你根本没有收到任何错误。 “未定义”实际上是根据您导出和发送给我的数据,这些记录的字段“描述”的内容。
执行此操作的流氓记录的“ID”如下: 97,96,95,94,86,85,84 按顺序排列。我想你或者其他人早些时候填补了这一点。另外,你应该看看@bfavaretto上面评论的内容。我认为这可能是原因。
我的测试源代码如下所示:
<?php
//@Author: ProfNoTime 062012 (Test Code - Not For Production Level Use)
$conn = mysql_connect("localhost", "root", "");
if (!$conn) { echo "Unable to connect to DB: " . mysql_error(); exit; }
if (!mysql_select_db("test")) { echo "Unable to select mydbname: " . mysql_error(); exit; }
$id = 1;
$sqlQuery = "select * from list_items where list_id='$id' order by position desc";
var_dump($sqlQuery);
$result=mysql_query($sqlQuery) or die(mysql_error());
if (!$result) { echo "Could not successfully run query ($sqlQuery) from DB: " . mysql_error(); exit; }
else{
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting...bye!"; exit;
}
else{
while($iteminfo=mysql_fetch_assoc($result)){
print_r($iteminfo);
var_dump($iteminfo);
}
}
}
mysql_free_result($result);
希望这有帮助。