php - 回显 MySQL 列的内容打印出 'undefined'

标签 php mysql

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

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

在某些行中,描述是空的,而在某些行中则不是。格式是mediumtext,但是当我把它改成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'] 仍然给出 'undefined'。

最佳答案

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

希望这对您有所帮助。

关于php - 回显 MySQL 列的内容打印出 'undefined',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11137581/

相关文章:

php - laravel 配置/数据库中 strict 的用途是什么?

mysql - 如何重写 UNION sql 查询,使其以 SELECT 开头

mysql - 为什么在正确的方法之后读取并不能产生一致的结果?

php - 使用 BINARY 查询 ZF2 模型

php - 如何使用 php 和 mysql 在 bootstrap div 中显示图像

php - 按键自动完成对 300 万行表的查询

php - 如何在 PHP 中使用条件将 .csv 文件上传到 MySql 表

php - 什么时候在 mysql 查询中评估变量?

phpMyAdmin 错误 : mysqli_real_connect(): (HY000/1045): Access denied for user 'pma' @'localhost' (using password: NO)

使用 MySQL 代理的 MySQL 故障转移