MySQL列的回显内容打印出“未定义”


Echo-ing content of MySQL column prints out 'undefined'

我有一个以前从未遇到过的奇怪问题。

我在一个名为 list_items 的 mysql 表中有一列description

在某些行中,

说明为空,在某些行中则不是。格式是中等文本,但是当我将其更改为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']仍然给出"未定义"。

>@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);

希望这有帮助。