0

I have an array $ziparray that I am trying to use in a MySQL query to check against a database column named 'zip'. The following code seems logical to me, but I am getting an error:

Catchable fatal error: Object of class stdClass could not be converted to string in...

$ziparrayimplode = implode(",", $ziparray);
$listinghistoryquery = "SELECT * FROM listings WHERE zip IN ($ziparrayimplode) ORDER BY list_ts DESC";
$listinghistory = mysql_query($listinghistoryquery) or die('SQL Error :: '.mysql_error());

I am absolutely certain the $ziparray is an array. Can anyone tell me how to make this query work? Here is the code used to create the array..

$countyzipquery = mysql_query("SELECT city_zip FROM dev_cities WHERE city_state='$state' AND city_county='$county' AND city_name='$city'");
$ziparray = array();
while (($ziparrayrow = mysql_fetch_object($countyzipquery)) !== FALSE) {
  $ziparray[] = $ziparrayrow;

Here is the output of var_dump($ziparray)

array(1) { [0]=> object(stdClass)#3 (1) { ["city_zip"]=> string(5) "63028" } }
4
  • 1
    Looks like your $ziparray is a standard object and not an array. Could you throw the code that you use to generate it? (Anonymize it if needed) Commented Nov 17, 2012 at 23:16
  • 1
    $ziparray might be an array, but it seems to contain at least one stdClass object, which fails to convert to a string for the implode operation itself. Please include the output of var_dump($ziparray) in your question. Commented Nov 17, 2012 at 23:16
  • 1
    Well there you go, you're fetching objects (mysql_fetch_object) into $ziparray and then you're trying to concatenate those objects. Commented Nov 17, 2012 at 23:18
  • Ok, I edited my post above with the info necessary. How can I fix this? This is a little over my head apparently. Commented Nov 17, 2012 at 23:22

1 Answer 1

2
while (($ziparrayrow = mysql_fetch_object($countyzipquery)) !== FALSE) {

You're fetching objects from your database and then storing those full objects into your $ziparray array. You cannot concatenate stdClass objects, because they cannot be converted to string (an object needs to define a __toString method for that, and stdClass instances do not have such a method).

You most likely want to fetch arrays (mysql_fetch_array instead of mysql_fetch_object) and then store only the first element ($ziparrayrow[0]) in your $ziparray. Alternatively, you can keep the mysql_fetch_object, but change your accumulation of elements in $ziparray:

$ziparray[] = $ziparrayrow->city_zip;
Sign up to request clarification or add additional context in comments.

1 Comment

THANK YOU! That's why I love this site. I always spend a couple of hours trying to figure it out on my own, but when I can't I come here and get a good lesson. Not only did that fix it, I now understand what I did wrong and learned a little about fetching arrays.

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.