我有一个字符串数组值,但是我不知道如何在sql语句中使用它。
我尝试使用内爆,但没有任何反应。
这是我的代码:
$select_tbl=mysqli_query($conn, "SELECT * FROM users WHERE firstname='$firstname' AND lastname='$lastname'");
while($fetch=mysqli_fetch_object($select_tbl)) {
$r = $fetch->lessons;
$i = explode(",",$r);
foreach ( $i as $item ) {
$string .= $item.',';
echo $string;
}
}
$_SESSION['library'] = $_GET['library'];
$_SESSION['lessonID'] = $_GET['lessonID'];
$_SESSION['department'] = $_GET['department'];
$dept = $_SESSION['dept'];
$library = $_SESSION['library'];
$checkID = mysqli_real_escape_string($conn, $_GET["lessonID"]);
$checkdept = mysqli_real_escape_string($conn, $_GET["department"]);
$department = mysqli_real_escape_string($conn, $_SESSION['dept']);
$sql = "SELECT * FROM gallery
WHERE lessonID = '$checkID' AND departmentg='$checkdept' AND library='$library' AND titleGallery='$string'";
$stmt = mysqli_stmt_init($conn);
我想在代码中获取 foreach 语句的字符串数组,以便可以将其用于 SELECT 中的 WHERE 子句>声明。
答案 0 :(得分:2)
这里您正在执行字符串到数组和数组到字符串的转换:
$r = $fetch->lessons; //string
$i = explode(",",$r); //array
foreach ( $i as $item ) { //array parsing
$string .= $item.','; //string
}
相反,您可以使用:
//declare string array before while loop
$string_array = array();
$string_array = array_merge($string_array,explode(',',$fetch->lessons)); //Final Array
您可以在sql查询中使用:
$sql = "SELECT * FROM gallery WHERE lessonID = '$checkID' AND departmentg='$checkdept' AND library='$library' AND titleGallery IN (".implode(',',$string_array).")";