使用数组在PHP函数中创建SQL语句

使用数组在PHP函数中创建SQL语句,php,arrays,Php,Arrays,我正在创建一个PHP函数,它将接受一些值,其中一个是数组,我需要在MySQL查询中使用这些值 我正在创建数组,如下所示: $newsArray = createArticleArray(array(2,20,3),5); 然后函数看起来像这样(为了可读性而减少) 我需要在tbl\u section.fld\u section\u uid=2或tbl\u section.fld\u section\u uid=20或tbl\u section.fld\u section\u uid=3部分使

我正在创建一个PHP函数,它将接受一些值,其中一个是数组,我需要在MySQL查询中使用这些值

我正在创建数组,如下所示:

 $newsArray = createArticleArray(array(2,20,3),5); 
然后函数看起来像这样(为了可读性而减少)

我需要在
tbl\u section.fld\u section\u uid=2或tbl\u section.fld\u section\u uid=20或tbl\u section.fld\u section\u uid=3
部分使用数组值

基本上,我需要循环遍历组成查询部分的数组中的值,但是我在如何显示或不显示“或”位方面遇到了一个小问题,因为可能只有1个值或我所需的任意多个值

我在想这样的事情:

 foreach($sectionArray as $section)
 {
   $sqlString = $sqlString . "tbl_section.fld_section_uid = $section OR";
 }
SELECT
...
WHERE tbl_section.fld_section_uid IN (2,20,3)
...
但我不知道如何确定是否要在其中添加“或”。

使用

此解决方案回答了您的问题,并向您展示了如何使用
内爆
函数,但对于您的具体情况,您应该真正使用操作符

$sqlString .= "tbl_section.fld_section_uid IN(".implode(',', $sectionArray).")";
使用

此解决方案回答了您的问题,并向您展示了如何使用
内爆
函数,但对于您的具体情况,您应该真正使用操作符

$sqlString .= "tbl_section.fld_section_uid IN(".implode(',', $sectionArray).")";

如果使用语法,查询可以变得更简单,更容易生成

使用PHP生成
(value1,value2,…)
部分:

$SQL .= ' WHERE tbl_section.fld_section_uid IN (' . implode(',', $array) . ') ';
产生如下结果:

 foreach($sectionArray as $section)
 {
   $sqlString = $sqlString . "tbl_section.fld_section_uid = $section OR";
 }
SELECT
...
WHERE tbl_section.fld_section_uid IN (2,20,3)
...

如果使用语法,查询可以变得更简单,更容易生成

使用PHP生成
(value1,value2,…)
部分:

$SQL .= ' WHERE tbl_section.fld_section_uid IN (' . implode(',', $array) . ') ';
产生如下结果:

 foreach($sectionArray as $section)
 {
   $sqlString = $sqlString . "tbl_section.fld_section_uid = $section OR";
 }
SELECT
...
WHERE tbl_section.fld_section_uid IN (2,20,3)
...

一种解决方案是在末尾添加一个无关的0来使用最终的“或”,而不产生任何效果。查询解析器只会删除它:
A或B或C或0
变成
A或B或C

另一种解决方案是使用
内爆
插入

$sqlString = "tbl_section.fld_section = "
 . implode($sectionArray," OR tbl_section.fld_section_uid = ");
当然,正确的解决方案只是在中使用

"WHERE tbl_section.fld_section_uid IN(".implode($sectionArray,',').")";

一种解决方案是在末尾添加一个无关的0来使用最终的“或”,而不产生任何效果。查询解析器只会删除它:
A或B或C或0
变成
A或B或C

另一种解决方案是使用
内爆
插入

$sqlString = "tbl_section.fld_section = "
 . implode($sectionArray," OR tbl_section.fld_section_uid = ");
当然,正确的解决方案只是在
中使用

"WHERE tbl_section.fld_section_uid IN(".implode($sectionArray,',').")";
函数createArticleArray($sectionArray=array(),$itemsToShow){
$conditions=array();
对于($i=0,$s=count($sectionArray);$i<$s;++$i){
$conditions[]='tbl_section.fld_section_uid='(int)$sectionArray[$i];
}
$SQL='SELECT*FROM tbl_section WHERE'。内爆('OR',$conditions)。'ORDER BY tbl_article.fld_date_created DESC LIMIT 0',(int)$itemsToShow;
}
函数createArticleArray($sectionArray=array(),$itemsToShow){
$conditions=array();
对于($i=0,$s=count($sectionArray);$i<$s;++$i){
$conditions[]='tbl_section.fld_section_uid='(int)$sectionArray[$i];
}
$SQL='SELECT*FROM tbl_section WHERE'。内爆('OR',$conditions)。'ORDER BY tbl_article.fld_date_created DESC LIMIT 0',(int)$itemsToShow;
}

使用PDO的准备方法:


使用PDO的准备方法:


你漏掉了几个括号。你漏掉了几个括号。这些是条件,不是子句。SELECT、FROM、WHERE是子句。这些是条件,而不是子句。选择、从、WHERE是子句。