douguio0185 2018-08-01 13:26
浏览 126
已采纳

如何从查询中设置对象属性?

I am trying to return three values to my front end. My problem is (or at least I think) is that it is returning as a boolean. Here is my code:

<?php
  require "../../inc/dbinfo.inc";

  ini_set("log_errors", 1);
  ini_set("error_log", "/tmp/php-error.log");
  error_log( "#################################################     FT-VENDOR-INVOICE-FILLOUT.PHP    #################################################" );

  $num = $_POST['num'];

  $sql = $conn->prepare("SELECT COUNT(*) AS sq FROM tblVendorInvoices WHERE VendorPOID1 = ?");
  $sql->bind_param("i",$num);
  $sql->execute();
  $hold = $sql->get_result();
  $obj->sq = $hold->fetch_object();
  $obj->sq = round($obj->sq,2);

  $sql2 = $conn->prepare("SELECT POAmount AS poam FROM tblVendorPOs WHERE VENDORPOID = ?");
  $sql2->bind_param("d",$num);
  $sql2->execute();
  $hold2 = $sql2->get_result();
  $obj->poam = $hold2->fetch_object();
  $obj->poam = round($obj->poam,2);

  $sql3 = $conn->prepare("SELECT SUM(VdrInvoiceAmount) AS itd FROM tblVendorInvoices WHERE VendorPOID1 = ?");
  $sql3->bind_param("d",$num);
  $sql3->execute();
  $hold3 = $sql3->get_result();
  $obj->itd = $hold3->fetch_object();
  $obj->itd = round($obj->itd,2);

  echo json_encode($obj);
  exit();
?>

My error log is reporting:

[01-Aug-2018 09:15:49 America/Toronto] #################################################     FT-VENDOR-INVOICE-FILLOUT.PHP    #################################################
[01-Aug-2018 09:15:49 America/Toronto] PHP Warning:  Creating default object from empty value in /var/www/html/fetch/ft-vendor-invoice-fillout.php on line 14
[01-Aug-2018 09:15:49 America/Toronto] PHP Notice:  Object of class stdClass could not be converted to int in /var/www/html/fetch/ft-vendor-invoice-fillout.php on line 15
[01-Aug-2018 09:15:49 America/Toronto] PHP Notice:  Object of class stdClass could not be converted to int in /var/www/html/fetch/ft-vendor-invoice-fillout.php on line 22
[01-Aug-2018 09:15:49 America/Toronto] PHP Notice:  Object of class stdClass could not be converted to int in /var/www/html/fetch/ft-vendor-invoice-fillout.php on line 29

Current Response: {"sq":1,"poam":1,"itd":1}

Desired Response is this exact format but the numbers are wrong. They are queried values so I'm sorry I can't give an exact output. I've tried typecasting some of these lines but to no success. I don't really understand why I'm getting this int conversion error in my log so an explanation of that would be amazing. The first value should always be an in and the second and third should be float.

Also, I couldn't understand most of PHP docs explanations so please don't reference those.

  • 写回答

1条回答 默认 最新

  • drpp5680 2018-08-01 13:34
    关注

    You have misunderstood what fetch_object() does. It returns an object, not a single field. You need to first reference the field, and then assign that to your object.

    It also appears that you can combine two of your queries into one, so below is a re-factored code base for you:

    <?php
    
    require '../../inc/dbinfo.inc';
    
    // Retrieve the POST var:
    $num = $_POST['num'];
    
    // Create our blank object:
    $obj = new stdClass();
    
    // Combines query 1 and 3:
    $sql = $conn->prepare('SELECT COUNT(*) AS sq, SUM(VdrInvoiceAmount) AS itd FROM tblVendorInvoices WHERE VendorPOID1 = ?');
    $sql->bind_param('i', $num);
    $sql->execute();
    $hold = $sql->get_result();
    $res = $hold->fetch_object();
    
    $obj->sq = round( $res->sq, 2 );
    $obj->itd = round( $res->itd, 2 );
    
    $sql2 = $conn->prepare('SELECT POAmount AS poam FROM tblVendorPOs WHERE VENDORPOID = ?');
    $sql2->bind_param('d', $num);
    $sql2->execute();
    $hold = $sql2->get_result();
    $res  = $hold->fetch_object();
    
    $obj->pam = round( $res->poam, 2 );
    
    // May also be handy to output JSON headers:
    header('Content-type: application/json');
    echo json_encode($obj);
    exit();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP