Jump to content

Storing An Array In A Variable


dinita

Recommended Posts

I've only been learning php for two weeks so please excuse my ignorance,

 

I would like to store an array of data from a mysql table in a variable to be called as and when its needed in my script.

 

i have written this function and used a vardump just to check wether the function was working:

function getquestions($id)
{
$sql    =mysql_query("select text FROM questions WHERE quiz_ID =$id ");
while($row   = mysql_fetch_array($sql))
{
 var_dump($row) ;
}
}
///Actual code
$quizid = get_id("quizName", "ID");
$questionid = get_id("questions","quiz_ID");
if ($quizid == $questionid)
{
$question = getquestions("1");
}

 

BUT my array lists all of my row values twice like this:

 

array(2) { [0]=> string(68) "The ‘Sea Swallow’ is an alternative name for which bird?" ["text"]=> string(68) "The ‘Sea Swallow’ is an alternative name for which bird?" } array(2) { [0]=> string(58) "In which sport would you see a ‘Western Roll’?" ["text"]=> string(58) "In which sport would you see a ‘Western Roll’?" } array(2) { [0]=> string(53) "Who is better known as ‘Herbert Khaury’?" ["text"]=> string(53) "Who is better known as ‘Herbert Khaury’?" } array(2) { [0]=> string(42) "'Diet' is the parliament of which country?" ["text"]=> string(42) "'Diet' is the parliament of which country?" } array(2) { [0]=> string(43) "What is the real first name of Coco Chanel?" ["text"]=> string(43) "What is the real first name of Coco Chanel?" } array(2) { [0]=> string(43) "'The Aztecs' were natives of which country?" ["text"]=> string(43) "'The Aztecs' were natives of which country?" } array(2) { [0]=> string(54) "What was invented by‘O.A. North’ in 1869?" ["text"]=> string(54) "What was invented by‘O.A. North’ in 1869?" } array(2) { [0]=> string(40) "King Zog was the ruler of which country?" ["text"]=> string(40) "King Zog was the ruler of which country?" }

 

I'd appreciate any explanation on how i stop this.

Link to comment
https://forums.phpfreaks.com/topic/269298-storing-an-array-in-a-variable/
Share on other sites

The var_dump($row) should be a clue.

 

fetch_array() returns effectively 2 arrays in one, one indexed by number and the other indexed by field name. You can specify a second parameter to choose one or the other (see manual) or you can use mysql_fetch_assoc() or mysql_fetch_row()

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.