从php数组中获取密钥并使用SQL WARE。。在里面子句来筛选结果
我有以下动态$array1:从php数组中获取密钥并使用SQL WARE。。在里面子句来筛选结果,php,mysql,arrays,Php,Mysql,Arrays,我有以下动态$array1: array(6) { [1]=> string(5) "false" [2]=> string(5) "true" [3]=> string(5) "false" [4]=> string(5) "false" [5]=> string(5) "true" [10]=&g
array(6) { [1]=> string(5) "false"
[2]=> string(5) "true"
[3]=> string(5) "false"
[4]=> string(5) "false"
[5]=> string(5) "true"
[10]=> string(5) "false"
}
希望你的问题很清楚。这两部分是你的问题 要过滤数组,可以使用
array\u filter()
,在使用字符串时,需要检查值是否为“true”以过滤掉“false”值
$array2 = array_keys(array_filter($array1, function($data) { return $data == "true"; }));
使用array\u keys()
获取索引列表
下一部分是构建SQL,然后绑定值。您应该为每个值构建一个具有适当数量的?
占位符的查询
SELECT sapp.appointment_id, sapp.time_start
FROM ss_appointments sapp
WHERE sapp.service_id IN (?,?) AND sapp.time_start >= ?
然后,绑定也将是(例如)
因此,它动态构建iis
数据类型和要绑定的字段列表。然后,使用参数unpacking(…
)操作符将它们放入
// Build comma separater list of placeholders
$in = trim(str_repeat("?,", count($array2)),",");
// Create appropriate bind data type string
$type= str_repeat("i", count($array2));
$sql = "SELECT sapp.appointment_id, sapp.time_start
FROM ss_appointments sapp
WHERE sapp.service_id IN (".$in.") AND sapp.time_start >= ?";
if ($result = $link->prepare($sql)) {
// Add the period to the bind data
$array2[] = $period_from;
$result->bind_param($type.'s', ...$array2);
$result->execute();
$result->bind_result($app_id, $time_start);
while($result->fetch()){
echo '..results here..';
}
}
已经有一段时间了,没有经过测试:
//filter out false, space and 0 and get keys
$params = array_keys(array_filter($array));
//add other variables in order of ? appearence
$params[] = $period_from;
//create a comma separated list of ?
$list = implode(',', array_fill(0, count($params), '?'));
//use $list in the IN clause
$result = $link->prepare("SELECT sapp.appointment_id, sapp.time_start
FROM ss_appointments sapp
WHERE sapp.service_id IN ($list) AND sapp.time_start >= ?";
//create types of string for each variable
$result->bind_param(str_repeat('s', count($params)), $params);
$array2=array\u键(array\u过滤器($array))代码>非常感谢,喜欢你的动态绑定字段列表解决方案!
$result->bind_param('iis', 2, 5, $period_from);
// Build comma separater list of placeholders
$in = trim(str_repeat("?,", count($array2)),",");
// Create appropriate bind data type string
$type= str_repeat("i", count($array2));
$sql = "SELECT sapp.appointment_id, sapp.time_start
FROM ss_appointments sapp
WHERE sapp.service_id IN (".$in.") AND sapp.time_start >= ?";
if ($result = $link->prepare($sql)) {
// Add the period to the bind data
$array2[] = $period_from;
$result->bind_param($type.'s', ...$array2);
$result->execute();
$result->bind_result($app_id, $time_start);
while($result->fetch()){
echo '..results here..';
}
}
//filter out false, space and 0 and get keys
$params = array_keys(array_filter($array));
//add other variables in order of ? appearence
$params[] = $period_from;
//create a comma separated list of ?
$list = implode(',', array_fill(0, count($params), '?'));
//use $list in the IN clause
$result = $link->prepare("SELECT sapp.appointment_id, sapp.time_start
FROM ss_appointments sapp
WHERE sapp.service_id IN ($list) AND sapp.time_start >= ?";
//create types of string for each variable
$result->bind_param(str_repeat('s', count($params)), $params);