【问题标题】:Decision making on MySQL query outputMySQL查询输出的决策
【发布时间】:2016-12-06 21:43:25
【问题描述】:

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

我尝试使用计数从第一个查询中获取整数值。它应该总结给定数据库表中匹配所有条件的条目的出现。此号码用符号p 签名。我想用mysql_result() 方法从输出中取回p 的值。

【问题讨论】:

  • 您将mysql_* 函数与mysqli_* 函数混合使用。那是行不通的。
  • 并且要明确一点:mysql_* 已弃用、旧的、已删除
  • @Jeff :谢谢,但mysqli_result() 似乎相同。我应该使用不同的功能吗?

标签: php mysql sql http-post


【解决方案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...

【讨论】:

  • 非常感谢您的帮助!
猜你喜欢
  • 2011-08-06
  • 2012-01-14
  • 1970-01-01
  • 1970-01-01
  • 2015-01-09
  • 1970-01-01
  • 2021-12-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多