strange print_r with php arrays
So i have this in my database:
INSERT INTO `strange` (`id`, `myKey1`, `myValue1`, `myKey2`, `myValue2`) VALUES
(1, 'sometext', 'somevalue', 'sometext', 'somevalue'),
(2, 'movie1', 'description1', 'movie2', 'description2');
with this code:
<?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 ( m开发者_C百科ysql_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);
?>
...the print_r for id 1 is: Array ( [sometext] => somevalue )
but with id 2 the output is: Array ( [movie1] => description1 [movie2] => description2 )
So can somebody please tell me what is going on and what i must be doing to get the all the data with id 1
With id 1, you have the same array key being used twice (sometext
), and so the array cannot differentiate between the two entries. The second one is overriding the first, making it look like there is only one entry.
Your second key/value pairing has the same key as the first, so its overwriting it.
Because in the first set of information myKey1 and myKey2 are identically. So you are essentially creating an array, inserting 'somevalue' into the key 'sometext', then immediately inserting the same value into the same key. No different than if you did..
$var = array();
$var['key'] = 'value';
$var['key'] = 'value';
精彩评论