我在这里遇到问题,我有这个查询:
function getPrelaciones($carnet){
$result = query(
"SELECT mhm.Materia_codigo1
FROM Materia_has_Materia mhm
, Materia m
, (SELECT mha.Materia_codigo as materianovista
FROM Materia_has_Alumno mha
WHERE mha.Alumno_carnet = '11111') as n
WHERE n.materianovista = mhm.Materia_codigo
AND mhm.Materia_codigo1 != ''
AND n.materianovista = m.codigo");
print json_encode($result);
}
它可以工作,但是当我尝试使其通用时,像下一个查询一样切换'11111'
for $carnet
,它不起作用,它工作得很好,但它返回一个空的json:
function getPrelaciones($carnet){
$result = query(
"SELECT mhm.Materia_codigo1
FROM Materia_has_Materia mhm
, Materia m
, (SELECT mha.Materia_codigo as materianovista
FROM Materia_has_Alumno mha
WHERE mha.Alumno_carnet = '%s' ) as n
WHERE n.materianovista = mhm.Materia_codigo
AND mhm.Materia_codigo1 != ''
AND n.materianovista = m.codigo", $carnet);
print json_encode($result);
}
有什么解决办法吗,谢谢
我的查询函数如下所示:
function query() {
global $link;
$debug = false;
//get the sql query
$args = func_get_args();
$sql = array_shift($args);
//secure the input
for ($i=0;$i<count($args);$i++) {
$args[$i] = urldecode($args[$i]);
$args[$i] = mysqli_real_escape_string($link, $args[$i]);
}
//build the final query
$sql = vsprintf($sql, $args);
if ($debug) print $sql;
//execute and fetch the results
$result = mysqli_query($link, $sql);
if (mysqli_errno($link)==0 && $result) {
$rows = array();
if ($result!==true)
while ($d = mysqli_fetch_assoc($result)) {
array_push($rows,$d);
}
//return json
return array('result'=>$rows);
} else {
//error
return array('error'=>'Database error');
}
}