0

I am looking to use the contents of an array

$arr =array(24,28,30,34, 40);

and pass these into the where clause of a MySQL select statement, all my research has shown this done by using IN to pass in all the array values in one go.

I need to pass in each array element one at a time and then echo out the results of the SQL statement one at a time as the select statement is updated with the next array element.

New to programming and PHP so just need a little example to get me started...

Thanks to Zad highlighted the real issue

I need to pass each array value individually to a SQL statement as these need to be utilised in Where clause with BETWEEN, eg. WHERE age BETWEEN $array1 AND $array2 in order to determine count over an age range

thanks for all the input

2
  • 3
    what code have you got so far? Commented Feb 21, 2012 at 6:05
  • So, you need ... WHERE age BETWEEN 24 AND 28, ... WHERE age BETWEEN 28 AND 30, etc? Commented Feb 21, 2012 at 7:23

4 Answers 4

1

You can use the implode function to build the string that contains the list;

$arr =array(24,28,30,34, 40);
$query = 'SELECT * FROM mytable WHERE id IN (' .implode($arr, ', '). ' )';
echo $query;`

http://codepad.org/tLPZxq8P

https://www.php.net/manual/en/function.implode.php

Sign up to request clarification or add additional context in comments.

10 Comments

Thanks.. but i need to pass in each element one at a time, I would use IN if i wanted to pass all array elements at once
Why do you need to pass one by one?, using IN is a lot faster since only requires to query the database once.
because i want to do a count of the number of records with each loop through
eg. SELECT COUNT(id) FROM table WHERE age = $array
after using the IN method you can just call the mysql_num_rows function and get the number of records. php.net/manual/en/function.mysql-num-rows.php
|
1

try it with escaping the argument

foreach($arr as $array_element) {
    $query = 'SELECT * FROM table WHERE field = \''.mysql_real_escape_string($array_element).'\'';
    //your statement
}

Comments

0

You can use a foreach function:

// make connection to mysql server
foreach ( $arr as $element ) {
    $statement = "SELECT whatever FROM wherever WHERE something = $element"; // maybe a little validation here wouldn't hurt either
    // execute statement
    // process results
} // end of foreach
// close connection

Comments

0
$arr =array(24,28,30,34, 40);
$a = 'SELECT * FROM foo WHERE bar IN('.implode(',',$arr).')';

Edit: I'll admit, I didn't fully read the question, the title is misleading - consider changing that.

I need to pass in each array element one at a time and then echo out the results of the SQL statement one at a time as the select statement is updated with the next array element.

Could you explain how the scenario a bit better?

Comments

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.