获取数组元素并使用php在mysql查询中使用它

时间:2022-01-13 03:35:57

I want to make Mysql query like this :

我想做这样的Mysql查询:

$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'数组元素更改my​​sql_query的WHERE变量。

$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

2 个解决方案

#1


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:

不确定为什么要这样做,特别是考虑到不推荐使用mysql_ *函数,但为了学习,在这种情况下你可以这样做:

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] ."' ");
}

#2


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.

我真的建议切换到PDO或mysqli。不仅不推荐使用mysql_ *函数而且非常容易进行sql注入,PDO和mysqli都允许您发送数组作为参数,并将为您处理引用封装。

#1


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:

不确定为什么要这样做,特别是考虑到不推荐使用mysql_ *函数,但为了学习,在这种情况下你可以这样做:

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] ."' ");
}

#2


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.

我真的建议切换到PDO或mysqli。不仅不推荐使用mysql_ *函数而且非常容易进行sql注入,PDO和mysqli都允许您发送数组作为参数,并将为您处理引用封装。