奇怪的print_r与php数组

时间:2010-08-13 12:44:29

标签: php sql

所以我在我的数据库中有这个:

INSERT INTO `strange` (`id`, `myKey1`, `myValue1`, `myKey2`, `myValue2`) VALUES
(1, 'sometext', 'somevalue', 'sometext', 'somevalue'),
(2, 'movie1', 'description1', 'movie2', 'description2');

使用此代码:

<?php
//error_reporting(0);
include "inclusions/entete.php";
$theID = $_GET['id'];
$filename = "fonctions-javascript/uploads/" . $theID. ".html";
//unlink($filename);


$query = "SELECT * FROM strange WHERE id = '$theID'";   
$result = mysql_query( $query ) or die ( mysql_error() );
$row = mysql_fetch_array( $result ); 
$contents = array($row['myKey1'] => $row['myValue1'], $row['myKey2'] => $row['myValue2']);

//$contents = array('one' => "something", 'two' => "something");
print_r ($contents);
//print_r($row);

?>

... id 1的print_r是:Array([sometext] =&gt; somevalue)

但是id为2,输出为:Array([movie1] =&gt; description1 [movie2] =&gt; description2)

有人可以告诉我发生了什么以及我必须做些什么才能获得id为1的所有数据

3 个答案:

答案 0 :(得分:3)

对于id 1,您使用了两次相同的数组键(sometext),因此数组无法区分这两个条目。第二个是覆盖第一个,看起来只有一个条目。

答案 1 :(得分:0)

你的第二个键/值配对与第一个相同,所以它会覆盖它。

答案 2 :(得分:0)

因为在第一组信息中myKey1和myKey2是相同的。所以你实际上是创建一个数组,在'sometext'键中插入'somevalue',然后立即将相同的值插入到同一个键中。没有什么不同,如果你这样做..

$var = array();
$var['key'] = 'value';
$var['key'] = 'value';