2

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

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']仍然给出'未定义'。

4

1 回答 1

1

@robert,我将其作为答案发布,因此您可以接受并关闭它。您根本没有收到任何错误。“未定义”实际上是根据您导出并发送给我的数据的那些记录的“描述”字段的内容。

执行此操作的流氓记录的“ID”如下:97、96、95、94、86、85、84,按 desc 顺序排列。我想你或其他人早些时候填写了这个。此外,您应该查看上面的@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);

希望这可以帮助。

于 2012-06-21T15:12:55.597 回答