0

I want to search the row(venue_id) of a database with an array ($valueIDArray).

Is this the right way to do this?

$query = "SELECT * FROM venue_booking
WHERE venue_id = $valueIDArray";
user3624883
  • 81
  • 2
  • 9
  • If venue (from your other question) and venue_booking are two tables in the same database you are much better off combining them into one query with a join, and avoiding any extra work for you the programmer, and for the php and sql servers too. – James May 13 '14 at 18:51

4 Answers4

0

Your code is vulnerable to something called SQL injection; you generally prevent these attacks by using prepared statements.

If you mean, "Is this SQL statement syntactically valid?" yes, but not for an array. You need to use the IN statement; see Passing an array to a query using a WHERE clause.

Community
  • 1
  • 1
Levi Morrison
  • 19,116
  • 7
  • 65
  • 85
0
$query = "SELECT * FROM venue_booking
          WHERE venue_id IN (" . implode(", ", $valueIDArray) . ")";
Pankaj Garg
  • 1,272
  • 15
  • 21
0

Assuming $valueIDArray values are numeric, implode into a comma separated list and use the IN clause:

$values = implode(',', $valueIDArray);
$query  = "SELECT * FROM venue_booking WHERE venue_id IN ($values)";

If text etc. you need quotes:

$values = "'". implode("','", $valueIDArray) ."'";
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
0

First make a comma separated list of IDs and then use IN clause of SQL.

$comma_separated = implode(",", $valueIDArray);

$query = "SELECT * FROM venue_booking WHERE venue_id in ($comma_separated)";

Nawed Khan
  • 4,393
  • 1
  • 10
  • 22