-1

I've looked at several other entries for how to use an array in a WHERE clause but I couldn't find the right solution for me. What I am trying to do is to create a drop down menu with dynamic values based on a Recordset containing $_SESSION variables.

What I want it to do is to list the possible classes in a drop down menu that a specific teacher is able to teach (so the list is dependant on what teacher is logged into the system). All the session variables needed work perfectly fine in itself, and as long as the session variables I put in my queries are single result strings there's no fault at all. But once I am encountering an array in my variables, that's where it's going wrong on me.

I read on other posts that in order to use an array in a WHERE clause, I have to create a function to convert the array into a string, as I've done below:

$subjectareajoin = implode(',', $_SESSION['SubjectArea']);


The query for the Recordset is as follows:

 $query_classlist = "SELECT DISTINCT Class FROM $table_name
 WHERE SubjectArea IN ('$subjectareajoin') AND CentreNo LIKE '".$_SESSION['CentreNo']."'
 ORDER BY Class ASC";
 $classlist = mysql_query($query_classlist) or die(mysql_error());
 $row_classlist = mysql_fetch_assoc($classlist);
 $totalRows_classlist = mysql_num_rows($classlist)


But it keeps telling me that there is an error with the Array to String conversion:

Notice: Array to string conversion in path\addteachertest.php on line 28


This is the initial query to create the subjectarea array:

$query_subjectarea = mysql_query("SELECT DISTINCT SubjectArea FROM $table_name
WHERE Initials = '".$_SESSION['Initials']."'
AND staff LIKE '%".$_SESSION['Surname']."'") or die(mysql_error());
$query_subjectarea_array = array();
while($row_query_subjectarea = mysql_fetch_array($query_subjectarea))
$query_subjectarea_array[] = $row_query_subjectarea;


This is var_dump($query_subjectarea_array):

array(1) { [0]=> array(2) { [0]=> string(15) "Expressive Arts" ["SubjectArea"]=> string(15) "Expressive 
Arts" } }


If anybody could tell me what is wrong with my approach that would be grand. Many thanks in advance!

4
  • Don't do this - it just screams SQL injection! Commented Oct 14, 2013 at 10:58
  • Use mysql_real_escape_string($value_no_injection) for ALL STRINGS inside queries. Commented Oct 14, 2013 at 11:00
  • I will look into protecting the code once I've actually got it working. Since I am a beginner with all this, I much prefer to work in the most basic way first so I can learn and then adapt once it's up and running, if that makes sense. So once I have the solution for this, I shall look into security. Commented Oct 14, 2013 at 11:03
  • The CentreNo represents the education centre the logged-in member of staff is employed, so this can not be anything but a unique string. Commented Oct 14, 2013 at 11:13

1 Answer 1

0

Please check that the item you are attempting to implode is actually an array or whether its an array of an array. You can verify that with a

echo"<pre>";
 print_r($_SESSION['SubjectArea']);
echo"</pre>";

What seems to be happening is -

while($row_query_subjectarea = mysql_fetch_array($query_subjectarea))
    $query_subjectarea_array[] = $row_query_subjectarea; // array appended to an array which gets you an array of arrays

What I believe you would want is -

while($row_query_subjectarea = mysql_fetch_array($query_subjectarea))
    $query_subjectarea_array[] = $row_query_subjectarea['SubjectArea']; // item appended to an array which gets you an array of subjects
Sign up to request clarification or add additional context in comments.

4 Comments

This comes out as a result: Array ( [0] => Array ( [0] => Expressive Arts [SubjectArea] => Expressive Arts ) ) Can I conclude from your answer in relation to this result, that the notion of Array within the Array is what is causing the issue? If so, what can I do to resolve that?
Please check the edited answer above. You need to add the actual item to the array instead of the entire array.
It's working fine now, thanks very much. I have been struggling a lot with getting the arrays to work but I see now what I have been doing wrong. I'll be able to get the others working as well :)
Arrays are the source of a lot of pain at the start but will soon become your best friend. Cheers!

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.