Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

In the project that I am creating, I have a mysql column that is an array of different pieces of data from the database. These pieces of data have info that I want to display. In order to separate these items in the array I used,

$get_query = mysql_query('Select array FROM data');
while($dataRow = mysql_fetch_assoc($getfrnd_query)) {
    $array = $dataRow['array'];
    if($array != "") {
        $data_from_array_column = explode("," $array);
        $getdata = mysql_query("SELECT * FROM info WHERE item = $data_from_array_column");
        //Then I used a mysql_fetch_assoc to get data based on the item.
    }
}

When I run this code, I get an error "Array to string conversion on the line with $getdata". Is there any way to get this to work?

Thank you.

share|improve this question
5  
Where does $data_from_array_column come into play? – Fred -ii- Apr 9 '14 at 0:07
    
This code takes the data associated with each item from the array mysql column and will eventually display it. – user3513120 Apr 9 '14 at 0:10
    
That error means that $data_from_array_column is an array, but you are using it as a string. Can you provide the code that generates it, or add var_dump($data_from_array_column); at the beginning of your code and post it so we can see it. – SuperScript Apr 9 '14 at 0:12
    
Showing your full code or reference taken from $data_from_array_column would shed a bit more light on the subject. ;-) Plus, "IF" anything, that should be wrapped in quotes. WHERE item = '$data_from_array_column'"); – Fred -ii- Apr 9 '14 at 0:15
    
I just changed the name of the variable with the explode function to $data_from_array_column. Would that fix my issue? – user3513120 Apr 9 '14 at 0:17

1 Answer 1

up vote 0 down vote accepted

The problem is that explode returns an array containing the strings in between the character(s) you used as a delimiter (, in your case), not a string.

PHP is getting mad because it doesn't know how to convert your array to a string automatically. So, you will need to convert it yourself. The options are:

  1. You want to select the row where item is equal to the nth element in the array, $data_from_array_column. If this is the case, you need to insert the following line of code after your explode:

    $data_from_array_column = $data_from_array_column[0];

  2. If you want to select where it matches any of the elements in the $data_from_array_column array, it will get more complicated. You would need to add this line after the explode:

    $data_from_array_column = implode("' OR item='",$data_from_array_column);

    and change your query on the next line to:

    $getdata = mysql_query("SELECT * FROM info WHERE item='$data_from_array_column'");

    This will create a MySQL query that looks some thing like this:

    SELECT * FROM info WHERE item='foo' OR item='bar' OR item='bla'

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.