来自$ MYSQLi-> fetch_row()的奇怪输出

时间:2011-02-19 20:50:27

标签: php mysql mysqli

这是MYSQLi代码:

  public function display() {
    $sql = "SELECT title, date_posted, text, url
              FROM notes ORDER BY date_posted DESC
              LIMIT ?, ?";
    $results = $this->query($sql, "ii",
                              $this->page_offset,
                              $this->notes_per_page);
    $results = $this->db->store_result();
    while ($row = $results->fetch_row()) {
      var_dump($row);
    }
    //$this->write($results);
  }

  // this is the $this->db->query() function referred to above.
  public function query() {
    $args = func_get_args();
    $statement = $this->db->prepare($args[0]);
    $args = array_slice($args, 1);
    call_user_func_array(array($statement, 'bind_param'), &$args);
    $statement->execute();
    return $statement;
  }

MYSQL表:

mysql> desc notes;
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| date_posted | date         | NO   |     | NULL    |                |
| title       | varchar(255) | NO   |     | NULL    |                |
| text        | longblob     | NO   |     | NULL    |                |
| url         | varchar(255) | NO   |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+

示例行:

mysql> SELECT title, url, date_posted FROM notes WHERE url='init';
+-------+------+-------------+
| title | url  | date_posted |
+-------+------+-------------+
| init  | init | 2011-02-16  |
+-------+------+-------------+
1 row in set (0.00 sec)

相应行的输出。世界上有什么......?:

  

array(4){[0] => string(0)“”[1] =>   string(0)“”[2] => string(4)“init”   [3] => string(4)“ ”}

如果我将fetch_array()切换为fetch_object(),我就明白了:

  

object(stdClass)#3(4){[“title”] =>   string(0)“”[“date_posted”] =>   string(0)“”[“text”] =>串(4)   “init”[“url”] => string(4)“ ”}

感谢您的所有帮助/建议/意见!

新观察:

向查询添加另一列会输出一个新列(尽管错误的列)。例如,如果:

// and yes, I do realize that I am repeating 'url',
// and I have no clue why this is happening.
$sql = "SELECT title, date_posted, text, url, url
              FROM notes ORDER BY date_posted DESC
              LIMIT ?, ?";

然后输出行是:

  

array(5){[0] => string(0)“”[1] =>   string(0)“”[2] => string(4)“init”   [3] => string(4)“ ”[4] =>   string(350)“

     

这是init文章的文本。为了简洁起见,我在这个stackoverflow问题中缩短了它   “}

4 个答案:

答案 0 :(得分:1)


    public function display() {
    $sql = "SELECT title, date_posted, text, url
              FROM notes ORDER BY date_posted DESC
              LIMIT ?, ?";
    $results = $this->query($sql, "ii",
                              $this->page_offset,
                              $this->notes_per_page);
    var_dump($results);
  }



 public function query() {
    $args = func_get_args();
    $statement = $this->db->prepare($args[0]);
    $args = array_slice($args, 1);
    call_user_func_array(array($statement, 'bind_param'), &$args);
    $statement->execute();
    $return = array();
$statement->store_result();
$row=array();
$data = $statement->result_metadata();
$fields = array();
$fields[0] = &$statement;
while($field = $data->fetch_field()) {
$fields[] = &$row[$field->name];
}
call_user_func_array("mysqli_stmt_bind_result", $fields);

$i = 0;
while ($statement->fetch()) {
foreach ($row as $key1=>$value1) $return[$i][$key1] = $value1;
$i++;
}
$statement->free_result();
return $return;


  }


答案 1 :(得分:0)

有可能因为你正在吹走语句对象(你通过重新分配引用它的变量来删除它),它会被垃圾收集并影响结果。尝试更改代码:

 public function display() {
    $sql = "SELECT title, date_posted, text, url
              FROM notes ORDER BY date_posted DESC
              LIMIT ?, ?";
    // Notice the changed variable name
    $stmt = $this->query($sql, "ii",
                              $this->page_offset,
                              $this->notes_per_page);
    $results = $this->db->store_result();
    while ($row = $results->fetch_row()) {
      var_dump($row);
    }
    //$this->write($results);
  }

答案 2 :(得分:0)

(编辑:oops我误读了你的部分代码。无论如何......)

在从预准备语句中获取数据之前,必须使用mysqli_stmt::bind_result()将变量绑定到fetch。然后,使用mysqli_stmt::fetch()获取每一行。

public function display() {     
    $sql = "SELECT title, date_posted, text, url
            FROM notes ORDER BY date_posted DESC
            LIMIT ?, ?";
    $results = $this->query($sql, "ii", 
                      $this->page_offset,
                      $this->notes_per_page);

    /* We have to BIND variables to store the result in */
    $row = new StdClass;
    $row->title = null;
    $row->date_posted = null;
    $row->text = null;
    $row->url = null;
    $results->bind_results($row->title, $row->date_posted, $row->text, $row->url);

    while (($status = $results->fetch()) === true) { 
        var_dump($row):
    }
    if($status === false) die("Error fetching data");
}  

答案 3 :(得分:0)

说实话,我无法让您的代码正常工作 - store_result()不断返回(bool)false。虽然,这对我来说很好:

class Sandbox {

    private $db;
    private $page_offset = 0;
    private $notes_per_page = 10;

    public function __construct() {
        $this->db = new mysqli('127.0.0.1', 'user', 'pass', 'test');
    }

    /**
     */
    public function display() {
        $sql = "
            SELECT title, date_posted, text, url
            FROM notes
            ORDER BY date_posted DESC
            LIMIT ?, ?
        ";
        $statement = $this->query($sql, 'ii', $this->page_offset, $this->notes_per_page);
        $results = $statement->get_result();
        while ($row = $results->fetch_row()) {
            var_dump($row);
        }
    }

    /**
     * @param string $sql
     * @param string $types
     * @param mixed  $arg,...
     * @return mysqli_stmt
     */
    public function query($sql, $types = null, $arg = null, $arg = null) {
        $args = func_get_args();
        array_shift($args);

        $statement = $this->db->prepare($sql);
        call_user_func_array(array($statement, 'bind_param'), &$args);
        $statement->execute();

        return $statement;
    }

}

$notes = new Sandbox();
$notes->display();

输出:

array(4) {
  [0]=>
  string(4) "init"
  [1]=>
  string(10) "2011-02-16"
  [2]=>
  NULL
  [3]=>
  string(4) "init"
}