dinita Posted October 24, 2012 Share Posted October 24, 2012 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); Quote Link to comment https://forums.phpfreaks.com/topic/269854-variable-works-when-echoed-but-not-when-returned/ Share on other sites More sharing options...
trq Posted October 24, 2012 Share Posted October 24, 2012 Care to format your code so that it is readable? Quote Link to comment https://forums.phpfreaks.com/topic/269854-variable-works-when-echoed-but-not-when-returned/#findComment-1387418 Share on other sites More sharing options...
dinita Posted October 25, 2012 Author Share Posted October 25, 2012 thanks for responding but this is solved! Quote Link to comment https://forums.phpfreaks.com/topic/269854-variable-works-when-echoed-but-not-when-returned/#findComment-1387699 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.