日期:2014-05-16  浏览次数:20476 次

细究从数据库返回的数据模样

标题有点绕,先说一下我为什么要高清楚从数据库得到的数据模样:

我这边是刚接触PHP,有的时候自己会追很多,一追就糊涂,跟这书上的列子做,太脑残了。所以就有了下面的测试。

1、首先,我们要插入数据库,用的是PHPMyadmin.

?

?

CREATE DATABASE match;

USE match;

CREATE TABLE IF NOT EXISTS `response` (
  `response_id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) DEFAULT NULL,
  `topic_id` int(11) DEFAULT NULL,
  `response` tinyint(4) DEFAULT NULL,
  PRIMARY KEY (`response_id`)
) ;


INSERT INTO `response` (`response_id`, `user_id`, `topic_id`, `response`) VALUES
(1, 17, 0, NULL),
(2, 17, 0, NULL);

?

?

2、然后创建showarray.php.

?

?

<?php
   // Start a session
  session_start();
  // Connect to the database
  $dbc = mysqli_connect();

  $query = "SELECT * FROM response WHERE user_id ='" . $_SESSION['user_id'] . "'";
  
  $data = mysqli_query($dbc, $query);
  
  while($row = mysqli_fetch_array($data)) {
    foreach ($row as $key => $value) {
      echo $key . '=>' . $value . '<br />';	
    }
  }
?>

?

?

3、最后的结果如下:

?

0=>1
response_id=>1
1=>17
user_id=>17
2=>0
topic_id=>0
3=>
response=>
0=>2
response_id=>2
1=>17
user_id=>17
2=>0
topic_id=>0
3=>
response=>

?

有点无聊,不过刚发现的时候我还是激动的。