2014-03-01 67 views
0

我有下面的PHP代碼:mysql_fetch_array接收3行,但返回4

$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`) 
+1

請在新代碼使用'mysql_ *'不要,它的正式棄用PHP 5.5.0的。相反,您應該使用[mysqli_ *](http://codular.com/php-mysqli)或[PDO](http://uk1.php.net/PDO)。查看所述API的比較[這裏](http://www.php.net/manual/en/mysqlinfo.api.choosing.php)。 –

回答

2

的原因是因爲你是在變量的聲明中加入的元素。而不是做的:

$blueprints [] = array(); 

$blueprints = array(); 
+0

哈哈哈......我知道這是愚蠢的:D謝謝 – AndreiBogdan

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

哈哈哈...我知道這是愚蠢的:D謝謝 – AndreiBogdan

相關問題