gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:13:06 25 4
gpt4 key购买 nike

这是 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"]=> string(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) "

This is the text for the init article. I cut it short for the sake of brevity in this stackoverflow question " }

最佳答案


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;


}


关于php - $MYSQLi->fetch_row() 的奇怪输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5053442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com