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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。