duan198727 2013-06-20 15:22
浏览 34

访问变量内的项目

I have the following schema of the database,

CREATE TABLE `tz_todo` (
  `id` int(8) unsigned NOT NULL auto_increment,
  `position` int(8) unsigned NOT NULL default '0',
  `text` varchar(255) collate utf8_unicode_ci NOT NULL default '',
  `dt_added` timestamp NOT NULL default CURRENT_TIMESTAMP,
  PRIMARY KEY  (`id`),
  KEY `position` (`position`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

If I am writing the following PHP statement,

$query = mysql_query("SELECT * FROM `tz_todo` ORDER BY `position` ASC");

How I access the text items inside $query?

Please guide. Thanks

UPDATE: I have tried this,

echo $query['id'];
echo $query['position'];
echo $query['text'];
  • 写回答

2条回答 默认 最新

  • dragonmeng2002 2013-06-20 15:29
    关注

    Attempting to print $query won't allow access to the information. One of the mysql result functions must be used: mysql_fetch_assoc(), mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.

    This is an example of how you would access your results using mysql_fetch_assoc():

     while ($row = mysql_fetch_assoc($result)) {
            echo $row['id'];
            echo $row['position'];
            echo $row['text'];
            echo $row['dt_added'];
        }
    

    It is good practice to first check if there are any results returned, below is an example of how you could do that:

    if (!$query) {
       //do stuff when there is no data returned
    }
    

    NOTE: mysql_query is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL should be used.

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?