本文介绍了获取数组元素并与PHP的MySQL查询中使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要让MySQL查询是这样的:
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' ");
我要改变使用'$ myArray的数组元素的mysql_query的WHERE变量。
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 ."' ");
}
}
或者是这样的:
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 ?
感谢
推荐答案
不知道为什么你正在试图做到这一点,特别是考虑到mysql_ *函数正在德precated,但对于学习的缘故,在这种情况下您的可能的做这样的事情:
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] ."' ");
}
这篇关于获取数组元素并与PHP的MySQL查询中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!