我尝试进行数据库查询,该查询通过查询来决定应该执行哪个查询。我使用此查询的输出来做出决定。请参阅下面我的 PHP
文件,其中包含所有 3 个查询。
<?php
require_once('dbConnect.php');
$studentid = $_POST['studentid'];
$classid = $_POST['classid'];
$date = $_POST['date'];
$signature = $_POST['signature'];
$sql = "SELECT count(case when studentid='$studentid' AND classid='$classid' AND endsig is NULL then 1 end) as p
FROM signature";
$r = mysqli_query($con,$sql);
$result = array();
while($row = mysqli_fetch_array($r)){
array_push($result,array(
'p'=>$row['p']
));
}
if(mysql_result($result, 2)==0){
$sql = "insert into signature (studentid,classid,start,startsig) values ('$studentid','$classid','$date','$signature')";
if(mysqli_query($con,$sql)){
echo 'success';
}
else{
echo 'failure';
}
}else{
$sql = "UPDATE signature SET endsig='$signature' WHERE startdate='$date' AND studentid='$studentid'";
if(mysqli_query($con,$sql)){
echo 'success';
}
else{
echo 'failure';
}
}
mysqli_close($con);
我尝试使用 count 从第一个查询中获取整数值。它应该总结给定数据库表中匹配所有条件的条目的出现情况。该数字用符号p
签名。我想使用 mysql_result()
方法从输出中获取 p
的值。
最佳答案
您在代码中执行了一些不必要的步骤。
mysqli_result
与 mysql_result
(从结果中获取字符串的方法)不同(一个类)。
$sql = "SELECT count(case when studentid='$studentid' AND classid='$classid' AND endsig is NULL then 1 end) as p FROM signature";
$r = mysqli_query($con,$sql);
// $result = array(); // not needed
// since we only will get one row anyway, there no need to loop through the results and push those to an array
// simply get the one row as array:
$row = mysqli_fetch_array($r);
// now you can work with that array and your wanted value 'p'
if($row['p']==0) {
// leave the rest as is...
关于php - MySQL 查询输出的决策,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41005471/