1

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

3
  • 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 Commented Sep 10, 2013 at 15:45
  • You are right, it was a typo. I have removed the extra " . thanks Commented Sep 10, 2013 at 15:48
  • Isn't this basically the same as your question from yesterday: stackoverflow.com/questions/18690267/… Commented Sep 10, 2013 at 16:14

2 Answers 2

1

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] ."' ");
}
Sign up to request clarification or add additional context in comments.

2 Comments

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
Sorry, i have not got 15 reputation yet ... :(
0

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.

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.