Jump to content

Variable Works When Echoed But Not When Returned


dinita

Recommended Posts

I'm trying to make it so that my code takes an id given at the beginning and uses this to return a set of arrays.

Can anyone explain why my code might work (meaning echo the desired arrays ) like this:

 

1Array ( [0] => The ‘Sea Swallow’ is an alternative name for which bird? [1] => In which sport would you see a ‘Western Roll’? [2] => Who is better known as ‘Herbert Khaury’? [3] => 'Diet' is the parliament of which country? [4] => What is the real first name of Coco Chanel? [5] => 'The Aztecs' were natives of which country? [6] => What was invented by‘O.A. North’ in 1869? [7] => King Zog was the ruler of which country? ) Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 )

 

when I echo the variable id and not when I use the return function? when the return function is used I recieve no errors but nothing is printed either.

 

Ideally I'd like the id variable to be returned as I don't really want to see it echoed.

I know its probably something really simple but I just can't work it out, any help would be much appreciated,

code below:

 

<?php
error_reporting(-1);
function getquestions($id)
{
$sql    =mysql_query("select text FROM questions WHERE quiz_ID =$id ");
$questions   = array();
while($row   = mysql_fetch_row($sql))
{
 $questions[] = $row[0];
}
return $questions;
}
function getquesid($id)
{
$sql    =mysql_query("select ID FROM questions WHERE quiz_ID =$id ");
$questions   = array();
while($row   = mysql_fetch_row($sql))
{
 $questions[] = $row[0];
}
return $questions;
}
//Connect to Database
$con = mysql_connect("localhost","dinita", "3nd3m0luk");
if(!$con)
{
die('Could not connect: '.mysql_error());
}
else
{
// SELECT DATABASE
mysql_select_db("quizCreation", $con);
}

//GET NAME
$name    ="my quiz";

   //get quiz id
$sql = mysql_query("SELECT ID FROM quizName WHERE Name= '$name'");
while ($row = mysql_fetch_array($sql))
{
 $id = $row['ID'];
}

echo $id;

$quizid    =$id;

$question    = getquestions($quizid);
$quesid    =getquesid($quizid);

print_r ($question);
print_r($quesid);
//use quiz id to output array of question ID's and text


// use question ID's to output 4 arrays
mysql_close($con);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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