关于MySQL查询输出的决策

时间:2016-12-06 21:43:25

标签: php mysql sql http-post

我尝试进行数据库查询,这使查询决定应该执行哪个查询。我使用此查询的输出来做出决定。请参阅下面的LIMIT offset, number文件,其中包含所有3个查询。

PHP

我尝试使用count从第一个查询中获取整数值。它应该总结给定数据库表中与所有条件匹配的条目的出现。此号码使用符号<?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); 签名。我希望使用p方法从输出中获取p的值。

1 个答案:

答案 0 :(得分:1)

您已在代码中完成了一些不必要的步骤。 mysqli_resultmysql_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...