Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have:

$array1 =     //contains places ids and locations;
$array2 = array();
$array3 = array();


  foreach($array1 as $itemz)
  {     
      $array2[] = $itemz[place][id];
      $array3[] = $itemz[place][location][city];

      $sql = "select * from places where id=".$array2." and location=".$array3."";
  }

But when I print $sql I get:

  select * from places where id=12 and location=Array

Can anyone please tell me what is wrong with the code?

Thanks!

share|improve this question
 
What output are you expecting? –  Shamim Hafiz Apr 19 '11 at 11:30
1  
I'm completely mystified as to how he/she got id=12 when $array2 is an array. –  JohnP Apr 19 '11 at 11:42
 
SQL injection warning: use mysql_real_escape_string() to protect yourself. –  Johan Apr 19 '11 at 11:44
add comment

4 Answers

up vote 1 down vote accepted

Why using array when you only need a standard variable :

$array1 =     //contains places ids and locations;

foreach($array1 as $itemz)
{     
    $id = $itemz['place']['id'];
    $city = $itemz['place']['location']['city'];

    $sql = "select * from places where id='$id' and location='$city'";
}
share|improve this answer
 
you're great! thanks!! –  user638009 Apr 19 '11 at 11:59
 
Please please please, read JohnP answer just below, I didn't notice about the lack of the quotes in your code. This is important, you have some serious problems in your code. I've updated my code too. –  Matthieu Napoli Apr 19 '11 at 12:30
add comment

I'm sorry but your code doesn't make sense at all. I'm surprised that you're getting that result at all. Let's walk through it.

Where are the quotes?

$array2[] = $itemz[place][id];
$array3[] = $itemz[place][location][city];

You're missing quotes here, please add them

$array2[] = $itemz['place']['id'];
$array3[] = $itemz['place']['location']['city'];

Array to String conversion

$sql = "select * from places where id=".$array2." and location=".$array3."";

This statement shouldn't work for 2 reasons.

  1. Assuming that id is a single field of INT and you have a bunch of INTs in $array2 you still can't compare them without a MySQL IN.

  2. You're converting from a PHP array to a string. That won't work.

Since you're running this in a loop $array2[] and $array3[] will continue to change and will grow.

So what you're actually trying to do is come up with a query like

$sql = "SELECT * 
        FROM places 
        WHERE 
             id IN (" . implode(',', $array2) . ") AND 
             location IN (" . implode(',', $array3) . ")";

But this makes no sense at all because as the loop continues you're retrieving the same data incrementally.

So I think what you actually want to do is

$sql = "SELECT * 
        FROM places 
        WHERE 
             id = {$itemz['place']['id']} AND 
             location = {$itemz['place']['location']['city']}";

This is most probably what you need. This retrieves the rows for each row as you iterate through you array.

A couple of improvements I would do is.

Run your query once after the looping is done so you only have to run the query one time and not n times.

Also, consider retrieving only the columns you need instead of doing SELECT *

share|improve this answer
 
I guess you mean columns instead of rows in your last sentence. Nevertheless +1. –  M42 Apr 19 '11 at 12:02
 
@m42 columns indeed, fixed :) –  JohnP Apr 19 '11 at 12:12
add comment

You can't use $array3 to build query, cause it is an array. Rather you can code like bellow -

 foreach($array1 as $i=>$itemz)
  {     
      $array2[$i] = $itemz[place][id];
      $array3[$i] = $itemz[place][location][city];

      $sql = "select * from places where id=".$array2[$i]." and location=".$array3[$i]."";
  }
share|improve this answer
add comment

This line:

 $array3[] = $itemz[place][location][city];

results in creating an array named $array3 and adding an element equal to $itemz[place][location][city] with a key of 0 to it. When you try to embed this variable into the query you have a problem because it's not a string.

What you probably need is:

 $id = $itemz['place']['id'];
 $city = $itemz['place']['location']['city'];
 $sql = "select * from places where id=".intval($id)." and location='".
        mysql_real_escape_string($city)."'";

Notice that I have made changes to fix some other serious problems with the code (indexing into arrays with constants instead of strings and leaving your code vulnerable to SQL injection).

share|improve this answer
add comment

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.