我的SQL是这样的:
$si_like_param="%".$si_gl_group_id."5";
$stmt = $db->prepare("select m1.id, m1.name, m1.surname, m1.fathername,
m1.region_num,m1.school_num,m1.variant,
m1.sinif, m1.qrup,m1.f_lan
from students_answers m1,
results m2
where
m1.id_exam=:si_exam_id
and m1.id=m2.st_answer_id
and m2.result_level >= :si_min_score
and m1.school_num in ('003','018','019','020')
and m1.region_num = 20
and m1.qrup like :si_group_id
order by m2.result_level desc;");
$stmt->bindparam(":si_exam_id", $si_exam_id);
$stmt->bindparam(":si_min_score", $si_min_score);
$stmt->bindparam(":si_group_id", $si_like_param,PDO::PARAM_STR);
$ si_gl_group_id变量可能为空。
在我的HTML中是这样的:
<option value="">All</option>
<option value="0">Zero</option>
<option value="1">One</option>
当我在phpmyadmin上执行此查询时,它可以工作,但在脚本中却不起作用。
最佳答案
您应该使用concat concat('%', :si_group_id, '5')
所以
$ si_like_param = $ si_gl_group_id;
$stmt = $db->prepare("select m1.id, m1.name, m1.surname, m1.fathername
, m1.region_num, m1.school_num,m1.variant,m1.sinif, m1.qrup,m1.f_lan
from students_answers m1, results m2
where m1.id_exam=:si_exam_id
and m1.id=m2.st_answer_id
and m2.result_level >= :si_min_score
and m1.school_num in ('003','018','019','020')
and m1.region_num = 20
and m1.qrup like concat('%', :si_group_id, '5')
order by m2.result_level desc;");
$stmt->bindparam(":si_exam_id", $si_exam_id);
$stmt->bindparam(":si_min_score", $si_min_score);
$stmt->bindparam(":si_group_id", $si_like_param,PDO::PARAM_STR);
并且您不应该使用旧的隐式联接sintax,而应将显式联接sintax用作
$stmt = $db->prepare("select m1.id, m1.name, m1.surname, m1.fathername
, m1.region_num, m1.school_num,m1.variant,m1.sinif, m1.qrup,m1.f_lan
from students_answers m1
INNER JOIN results m2 ON m1.id=m2.st_answer_id
where m1.id_exam=:si_exam_id
and m2.result_level >= :si_min_score
and m1.school_num in ('003','018','019','020')
and m1.region_num = 20
and m1.qrup like concat('%', :si_group_id, '5')
order by m2.result_level desc;");
$stmt->bindparam(":si_exam_id", $si_exam_id);
$stmt->bindparam(":si_min_score", $si_min_score);
$stmt->bindparam(":si_group_id", $si_like_param,PDO::PARAM_STR);
关于php - 将空值传递给mysql一样的查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53633255/