dongluzhi5208 2014-02-18 00:58
浏览 48
已采纳

将变量值与PHP中的查询结果进行比较[重复]

I'm trying to pull all the "totalsDate" values from a table "totals" and then either INSERT a new record or UPDATE an existing one based on a variable $date.

//getting all the dates from the totals table and assigning to row
    $sqlDate = "SELECT totalsDate FROM totals";
    $query = mysqli_query($dbCon, $sqlDate);
    //$row = mysqli_fetch_array($query);

    while($row = mysqli_fetch_array($query)){
      $rowDate = $row['totalsDate'];

       //if statement to either update the totals table or create a new record
      if($rowDate = $date){

      $sqlThree = "UPDATE totals SET lodgements = '$lodgementsAfter' WHERE branch_name = '$branchTest' AND totalsDate = '$date'";
      $query = mysqli_query($dbCon, $sqlThree);

      }

      else {
      $sqlFour = "INSERT INTO totals VALUES(NULL, '$branchTest', 0, '$amount', 0,  '$date')";
      $query = mysqli_query($dbCon, $sqlFour);
    }



    }

The update part works, however my else statement will never be executed and a new record cannot be entered. I also get this error:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in

I'm a bit confused on using the mysqli_fetch_array and how to actually use the data?

Thanks for any help.

</div>
  • 写回答

2条回答 默认 最新

  • dsa45132 2014-02-18 01:03
    关注

    your db query is not correct. You are not getting a good result back from your query. The error is saying it expects the result to be an array and its not. its returning a true of false.

    read this mysqli_query

    for more help.

    do you have access to the db command line? what does the query return. Are you certain your db connection info is correct?

    i found it.

     $rowDate = $date   should be $rowDate == $date or $rowDate === $date
    

    your sql result is an array

    $row[0] = "1st result"
    $row[1] = "2nd result"
     ..etc
    

    so theortically if you have more then 1 result, it should be doing multiple updates/inserts. are you seeing that? if its just one result then clearly 2014-02-13, 2014-02-26 are not the same.

     $sqlDate = "SELECT totalsDate FROM totals where totalsDate='" . $date . "'";
    

    this will return results, with only records that have $date.

     $rowcount=mysqli_num_rows($query);
    if ($rowcount) < 1))
    {
       insert;
    }
    else
    {
       update;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据