tags:

views:

51

answers:

3

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 ( 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);

?>

...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

+3  A: 

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.

Edward Mazur
hmmm, yes, so is there any way i can override it and force it to display all, even if it duplicates?
mahen23
Not using just arrays. Some ideas would be to wrap the key/value pairs inside objects or additional arrays (e.g. array(array(key => value), array(key => value))).
Edward Mazur
A: 

Your second key/value pairing has the same key as the first, so its overwriting it.

Visage
A: 

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';
Cags