dongma2388 2016-12-06 21:43
浏览 32
已采纳

关于MySQL查询输出的决策

I tried to make a database query, which makes a query to decide which query should be executed. I use output of this query to make decision. Please see my PHP file below, which contains all the 3 queries.

<?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);

I try to get an integer value from the first query with use of count. It should summarize occurences of entries in the given database table that match all the conditions. This number is signed with symbol p. I would like to get back value of p from the output with mysql_result() method.

  • 写回答

1条回答 默认 最新

  • dqask02082 2016-12-07 17:44
    关注

    You've done some unnecessary steps in your code. And mysqli_result is something different (a class) to mysql_result(a method to get strings from a 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...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像