dinita Posted October 10, 2012 Share Posted October 10, 2012 is it possible to store the keys of an array in to variables? currently my code looks like this: function getanswers() { $sql = mysql_query("select answer FROM answers WHERE question_ID= 1 "); while($row =mysql_fetch_row($sql)) { //print_r (array_keys($row)); $ans1 = $row['0']; $ans2 = $row; $ans3 = $row; $ans4 = $row; echo $ans1 . $ans2. $ans3. $ans4 ; } } and all I get are undefined offset notices what am i doing wrong? I'd appreciate any advice the output looks like this: The ‘Sea Swallow’ is an alternative name for which bird? Notice: Undefined offset: 1 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 29 Notice: Undefined offset: 2 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 30 Notice: Undefined offset: 3 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 31 Seagull Notice: Undefined offset: 1 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 29 Notice: Undefined offset: 2 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 30 Notice: Undefined offset: 3 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 31 Penguin Notice: Undefined offset: 1 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 29 Notice: Undefined offset: 2 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 30 Notice: Undefined offset: 3 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 31 Tern Notice: Undefined offset: 1 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 29 Notice: Undefined offset: 2 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 30 Notice: Undefined offset: 3 in /Applications/MAMP/htdocs/Quiz/newfile.php on line 31 Cormorant Just in case it helps when I call the array keys they show this: Array ( [0] => 0 )Array ( [0] => 0 )Array ( [0] => 0 )Array ( [0] => 0 ) Thanks in advance to anyone who can help! Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted October 10, 2012 Share Posted October 10, 2012 Your code doesn't match the errors. Please show the actual code you're using. Quote Link to comment Share on other sites More sharing options...
dinita Posted October 10, 2012 Author Share Posted October 10, 2012 here is the full code //functions function get_id( $table) { $sql = mysql_query("select * FROM $table") ; while ($row =mysql_fetch_array($sql)) { return $row['ID']; } } function getquestions($id) { $sql =mysql_query("select text FROM questions WHERE quiz_ID =$id "); while($row = mysql_fetch_row($sql)) { return $row ; } } function getanswers() { $sql = mysql_query("select answer FROM answers WHERE question_ID= 1 "); while($row =mysql_fetch_row($sql)) { print_r (array_keys($row)); $ans1 = $row['0']; $ans2 = $row['1']; $ans3 = $row['2']; $ans4 = $row['3']; echo $ans1 . $ans2. $ans3. $ans4 ; } } //Connect to Database $con = mysql_connect("localhost","dinita","8888888"); if(!$con) { die('Could not connect: '.mysql_error()); } else { // SELECT DATABASE mysql_select_db("quizCreation", $con); ///Actual code $quizid = get_id("quizName", "ID"); $questionid = get_id("questions","quiz_ID"); if ($quizid == $questionid) { $question = getquestions("1"); echo $question[0]; getanswers(1); mysql_close($con); } } Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 10, 2012 Share Posted October 10, 2012 You're selecting only one field in each record. There will never be an array key higher than 0, but it will be present once for however many records the query matches. Quote Link to comment Share on other sites More sharing options...
dinita Posted October 10, 2012 Author Share Posted October 10, 2012 ok, I understand so how would I go about assigning each of these values to different variables? or should I be calling more than one field in each record? Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted October 10, 2012 Share Posted October 10, 2012 1) You were using ['0'] which is a string. [0] is a number. 2) Don't ever use sequentially numbered variables. You want to use arrays Quote Link to comment Share on other sites More sharing options...
dinita Posted October 10, 2012 Author Share Posted October 10, 2012 thanks for all your help, but changing the numbers from strings to a number makes no difference, I still receive the same Notices. I understand that its probably bad practice to use sequentially numbered variables but I haven't really been having much luck with arrays, I've only been learning php for 2 weeks, so please excuse my lack of knowledge. As I tried to explain I'm trying to understand how to call each part of the array, at the moment this is causing undefined notices. maybe it would help if I explained what I was trying to do, this file will be sending variables to dynamic text fields in flash that will display the questions and answers from my database. Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted October 10, 2012 Share Posted October 10, 2012 That was a general tip about correct code, your code is still not written right because you're selecting 1 column and expecting 4. You want something like: $sql = mysql_query("select answer FROM answers WHERE question_ID= 1 "); $answers = array(); while($row =mysql_fetch_row($sql)) { $answers[] = $row[0]; } echo "The answers are: " . implode(', ', $answers); -Dan Quote Link to comment Share on other sites More sharing options...
dinita Posted October 10, 2012 Author Share Posted October 10, 2012 Thank you, Dan and everyone else for your input! Quote Link to comment 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.