mysql_fetch_array接收3行,但返回4

时间:2014-03-01 18:35:55

标签: 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`)

2 个答案:

答案 0 :(得分:2)

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

$blueprints [] = array ();

$blueprints = array();

答案 1 :(得分:1)

$blueprints [] = array ();  // <------ this is that