www.webdeveloper.com
Results 1 to 3 of 3

Thread: variable array is only populating last row of mysql table

Hybrid View

  1. #1
    Join Date
    Jul 2012
    Posts
    13

    variable array is only populating last row of mysql table

    I am calling back the results for AnswerStatusID and AnswerResponse and need to apply them to a variable to compare against to see if the answer given is correct or incorrect and the corresponding response for the answer, my issue is that my variable is only being populated by the last row in the table instead of populating it with all of the data.

    // Connect to the Database
    require_once('mysqli_connect.php');

    //create the query for the question
    $q = "SELECT `Question` FROM tbl_Question WHERE QuestionID = 1";

    //Create the query for the Answers
    $q2 = "SELECT `Answer`,`AnswerStatusID`,`AnswerResponse` FROM tbl_Answer WHERE QuestionID = 1";


    //Run the query
    $r = mysqli_query($conn,$q);

    //run the answer query
    $r2 = mysqli_query($conn,$q2);


    while($row = mysqli_fetch_array($r,MYSQLI_ASSOC)){
    echo '<div id="Question1"><p>1) ' . $row['Question'] . '</div></p>';
    }



    while($row2 = mysqli_fetch_array($r2,MYSQLI_ASSOC)){
    echo '<div id="Question1"><input name="q1" type="radio" value="'.$AnswerStatusID.'"/>' . $row2['Answer'] . '</div><br/>';

    //Assign the AnswerStatusID to a var
    $AnswerStatusID = $row2['AnswerStatusID'];

    //Assign the AnswerResponse to a var
    $AnswerResponse = $row2['AnswerResponse'];
    }

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,523
    Did you want those to be arrays? If so, use the square bracket notation to append elements to those arrays. Right now you are treating them as scalar variables, so they can only hold the last value assigned to them.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jul 2012
    Posts
    13
    got it thanks!!

    //Declare the variables as a array
    $AnswerResponse = array();
    $AnswerStatusID = array();

    while($row2 = mysqli_fetch_array($r2,MYSQLI_ASSOC)){
    echo '<div id="Question1"><input name="q1" type="radio" value="'.$AnswerStatusID.'"/>' . $row2['Answer'] . '</div><br/>';

    //Assign the AnswerStatusID to a var
    $AnswerStatusID[] = $row2['AnswerStatusID'];

    //Assign the AnswerResponse to a var
    $AnswerResponse[] = $row2['AnswerResponse'];
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles