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

I want to make Mysql query like this :

$sql = mysql_query("SELECT * FROM myTable WHERE id='11' AND number='23' AND value='45' AND result='101' ");

I want to change the WHERE variable of mysql_query using '$myArray' array element.

$myArray[0] = array(11, 23, 45, 101);  => this is the current query
$myArray[1] = array(21, 31, 70, 58);
$myArray[2] = array(8, 77, 68, 94);

I tried to get result like this :

foreach($myArray[] as $singleRow) {
  foreach($singleRow as $myElement) { 
    $sql = mysql_query("SELECT * FROM myTable WHERE id='". $myElement ."' AND number='". $myElement ."' AND value='". $myElement . "' AND result='". $myElement ."' ");
  }
}

Or like this :

for ($i=0; $i<count($myArray); $i++) {
  foreach($myArray[$i] as $myElement) {
    $sql = mysql_query("SELECT * FROM myTable WHERE id='". $myElement ."' AND number='". $myElement ."' AND value='". $myElement . "' AND result='". $myElement ."' ");
  }
}

Both are wrong ... How to do the right one ?

Thanks

share|improve this question
1  
Use PDO or ADOdb since mysql_query is deprecated. With those you can execute a query by passing an array as parameter. Look into it :) Also, your $myElement at the end has an extra " that would cause it to fail –  Mr Jack Sep 10 '13 at 15:45
 
You are right, it was a typo. I have removed the extra " . thanks –  user2758001 Sep 10 '13 at 15:48
 
Isn't this basically the same as your question from yesterday: stackoverflow.com/questions/18690267/… –  Barmar Sep 10 '13 at 16:14
add comment

2 Answers

up vote 1 down vote accepted

Not sure why you are trying to do this, especially considering mysql_* functions are being deprecated, but for the sake of learning, in this instance you could do something like this:

foreach($myArray as $row) { 
   $sql = mysql_query( "SELECT * FROM myTable WHERE id='". $row[0] ."' AND number='". $row[1] ."' AND value='". $row[2] . "' AND result='". $row[3] ."' ");
}
share|improve this answer
 
Thanks for your answer cillosis. Actually what i want to do is this : *) i have a table with more 100.000 rows. It has "id" column (auto_increment) and "value" column (int 0 or 1). *) i want to looking for how many presence that match the binary pattern 2^10 (2 power of 10) that are "0000000000", "0000000001", "0000000010", "0000000011", ...... "1111111111". *) This pattern can be searched using (for 3 digit pattern) select count(*) match_count from TheTable t1 join TheTable t2 on t1.id+1 = t2.id join TheTable t3 on t1.id+2 = t3.id where t1.value = 1 and t2.value = 0 and t3.value = 1 –  user2758001 Sep 10 '13 at 15:55
 
If my answer was useful, you can check the checkmark to accept it. –  cillosis Sep 10 '13 at 15:58
 
Sorry, i have not got 15 reputation yet ... :( –  user2758001 Sep 10 '13 at 16:06
 
Done ... thank you –  user2758001 Sep 11 '13 at 8:23
add comment

You're adding in too many loops, and not referencing the right variables at the right time:

foreach($myArray as $singleRow) {
    $sql = mysql_query("SELECT * FROM myTable WHERE id='". $singleRow[0] ."' AND number='". $singleRow[1] ."' AND value='". $singleRow[2] . "' AND result='". $singleRow[3] ."' ");
}

I'd really recommend switching over to PDO or mysqli. Not only are mysql_* functions deprecated and very prone to sql injection, both PDO and mysqli allow you to send an array as the parameters and will take care of the quote-encapsulation for you.

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.