1

I have a couple of issues with the following code... Essentially, several values will need to be stored in $sqlBAnswer, but if I simply put [] after it, it saves the value "Array".

//Find the answer given by the user to the last answered question
$sqlB = mysql_query("SELECT Answer FROM Responses WHERE User = $sqlAPKID");
//If the operation produces an error, output an error message
if (!$sqlB) {
    die('Invalid query for SQLB: ' . mysql_error());
}
//Count the number of rows output
$sqlBCount = mysql_num_rows($sqlB);
//If rows exist, define the values
if ($sqlBCount > 0) {
    while ($row = mysql_fetch_array($sqlB)) {
        $sqlBAnswer = $row["Answer"];
    }
}

Assuming $sqlBAnswer did manage to hold multiple values, I then need to execute another query that will produce only one value (ie only one of the values stored in $sqlBAnswer will be in the result set.

I plan to do this using a foreach loop around the following code:

//Find the number of the next question to be answered based on the user's previous answer and the question they answered
$sqlC = mysql_query("SELECT NextQuestion FROM Answers WHERE QuestionNumber = $sqlALastQuestionAnswered AND PKID = $sqlBAnswer");
//If the operation produces an error, output an error message
if (!$sqlC) {
    die('Invalid query for SQLC: ' . mysql_error());
}
//Count the number of rows output
$sqlCCount = mysql_num_rows($sqlC);
//If rows exist, define the values
if ($sqlCCount > 0) {
    while ($row = mysql_fetch_array($sqlC)) {
        $sqlCNextQuestion = $row["NextQuestion"];
    }
}

What I need to have at the end is one value and one value only for sqlCNextQuestion, but I can't wrap my head around keys and values and whatnot, however much I read the documentation. If anyone could explain and show me how I could achieve what I'm after, I'd be very grateful!

Thanks :)

4
  • 1
    please use PDO or MYSQLI instead of MYSQL Commented Jan 29, 2013 at 9:13
  • Why not just do a JOIN in MySQL itself? Commented Jan 29, 2013 at 9:16
  • What You Mean it saves the value "Array". When you Put "[]" after $sqlBAnswer it make Array of $row["Answer"] After This code with print_r($sqlBAnswer); You Can See This is Array Of $row["Answer"] for sort You Can Use asort,sort,... In PHP or Use Order In query Commented Jan 29, 2013 at 9:17
  • you can use array_push instead of put "[]" Commented Jan 29, 2013 at 9:28

2 Answers 2

3

At the moment in your code, $sqlBAnswer is not an array, but just a normal variable. Your code:

if ($sqlBCount > 0) {
    while ($row = mysql_fetch_array($sqlB)) {
        $sqlBAnswer = $row["Answer"];
    }
}

simply loops through the rows in the query result and re-assigns the value of $row["Answer"] to $sqlBAnswer in every row.

If you want to save those values to an array, you simply do this:

$sqlBAnswer = array(); //that creates a blank array to assign values to
if ($sqlBCount > 0) {
    while ($row = mysql_fetch_array($sqlB)) {
        $sqlBAnswer[] = $row["Answer"]; //note the '[]', which tells php to add the new value to the array
    }
}

You can then do your foreach in the following way:

foreach($sqlBAnswer as $value){
    // use your code with $sqlBAnswer substituted by $value
}

However - as to how you will select which value of $sqlCAnswer you want in the end, you have not described adequately what you want exactly for me to answer that. This code will loop through all values of $sqlBAnswer and probably produce many values of $sqlCAnswer (depending on your database) - so you'll need to refine your question or figure out yourself how to solve that problem.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Chid, I will try this now :) In regards to the last part of your question, I'll explain a little differently... $sqlBAnswer will produce 5 values (for instance). Out of these 5 values, only one will be present in the table I am querying to find $sqlCNextQuestion. I just need a way to pull that one value out. I hope that makes sense!
I've just given this a go Chid, but it's still reading $sqlCNextQuestion as an empty set and therefore redirecting to another page. It's almost as if it's either not finding that value at all, or it's finding it and then being overwritten by an empty set...
OK, this has worked now - THANK YOU! I was only putting the query in the foreach loop and not the whole of the second code block.
0

Solution to problem 1

$sqlBAnswer = $row["Answer"];

Should be

$sqlBAnswer[] = $row["Answer"];

I know you mentioned that it just stores "Array". But that is not true, it creates the array properly just that you are accessing it incorrectly here

$sqlC = mysql_query("SELECT NextQuestion FROM Answers WHERE QuestionNumber = $sqlALastQuestionAnswered AND PKID = $sqlBAnswer");  // No index being provided for array access

In a foreach as you mentioned, this will be like as follows

foreach($sqlBAnswer as $ans)
{
$sqlC = mysql_query("SELECT NextQuestion FROM Answers WHERE QuestionNumber = $sqlALastQuestionAnswered AND PKID = $ans");
}

3 Comments

Thanks for your answer - this is what I've tried before, and through error reporting, as soon as submit and it tries to find the next question, I get this error message: "Invalid query for SQLC: Unknown column 'Array' in 'where clause'" - Hence why I thought it was storing an array! Any suggestions?
What is the value of $sqlALastQuestionAnswered
That would be an integer. 1, for instance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.