我有以下 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/