php - MySQL 查询输出的决策

标签 php mysql sql http-post

我尝试进行数据库查询,该查询通过查询来决定应该执行哪个查询。我使用此查询的输出来做出决定。请参阅下面我的 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_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...

关于php - MySQL 查询输出的决策,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41005471/

相关文章:

javascript - 无法让 Select2 从我的 mysql 数据库获取数据

php - 有没有办法将当前时间与mysql保存的时间进行比较

php - 回显用户喜欢的类别的分层MySQL表

mysql - 我怎样才能做左连接但有两个子句

php - 将数据放入主 Blade - Laravel

javascript - 如何通过javascript将html代码的select-option值传递到php文件中?

java - 根据文本字段值修改 select 语句

php - 检查 MySQL 数据库中是否存在电子邮件时出错?

php - 匹配到单行

mysql - 查找重复条目并更新列 - Mysql