php - mysql_fetch_array 接收 3 行,但返回 4

标签 php mysql sql

我有以下 PHP 代码:

$queryBlueprint = "SELECT * FROM `plan_photos` WHERE project_id=" . $projectId;
$resultBlueprints = mysql_query ( $queryBlueprint );
if ($resultBlueprints) {
    // Add the results into an array
    $blueprints [] = array ();
    echo mysql_num_rows ( $resultBlueprints ); /* --- this returns: 3 */
    while ( $row = mysql_fetch_array ( $resultBlueprints ) ) {
        $blueprints [] = $row;
    }
    echo "<br/>";
    echo count ( $blueprints ); /* --- this returns 4*/
    echo "<br/>";
    echo print_r ( $blueprints [0] );
    echo "<br/>";
    echo print_r ( $blueprints [1] );
    echo "<br/>";
    echo print_r ( $blueprints [2] );
    echo "<br/>";
    echo print_r ( $blueprints [3] );

为什么 mysql_num_rows 返回 3,但在我将每个结果添加到一个数组后,该数组包含 4 个项目?第一个 ([0]) 是“null”,接下来的 3 个 ([1]、[2] 和 [3]) 是它们应该的样子(也就是它们包含数据)

回显数据:

3
4
Array ( ) 1 /*    <------ what is this?!?!    */ 
Array ( [id] => 8 [project_id] => 2 [photo] => http://webja5309b6cf8a525.jpg [title] => first ) 1
Array ( [id] => 9 [project_id] => 2 [photo] => http://webja7ee76.jpg [title] => second ) 1
Array ( [id] => 10 [project_id] => 2 [photo] => http://webj022d3.jpg [title] => third blueprint ) 1

表格如果有帮助:

CREATE TABLE IF NOT EXISTS `plan_photos` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `project_id` int(11) NOT NULL,
  `photo` text NOT NULL,
  `title` varchar(50) NOT NULL,
  KEY `project_id` (`id`),
  KEY `project_id_2` (`project_id`)

最佳答案

原因是因为你在变量的声明中添加了一个元素。而不是做:

$blueprints [] = array ();

$blueprints = array();

关于php - mysql_fetch_array 接收 3 行,但返回 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22118568/

相关文章:

php - 如何监听UDP并使用PHP

mysql存储过程比标准查询慢20倍

一个查询中的 MySQL 一对多关系

php - PostgreSQL:在特定数据库中创建模式 [NOT psql]

sql - 为什么 Postgres 告诉我当两个表都在同一数据库上时跨数据库引用不可用?

php - 使用meta_key 对帖子元数据进行排序?

php - 处理数据库连接中的各种错误-有些不起作用?

javascript - CKEditor 的上传图片插件有回调函数吗?

mysql - 删除记录

Mysql查询涉及多张表和一个OR语句