如何通过查询设置对象属性?

时间:2018-08-01 13:26:01

标签: php mysql

我正在尝试将三个值返回到前端。我的问题是(或者至少我认为)它以布尔值形式返回。这是我的代码:

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

我的错误日志正在报告:

[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

当前响应:{"sq":1,"poam":1,"itd":1}

所需的响应是这种确切格式,但数字有误。它们是查询的值,所以很抱歉我无法提供确切的输出。我尝试过强制转换其中一些行,但没有成功。我真的不明白为什么我的日志中会出现此int转换错误,因此对此做出的解释将是惊人的。第一个值应始终为in,第二个和第三个值应为float。

此外,我听不懂大多数PHP文档说明,因此请不要引用这些说明。

1 个答案:

答案 0 :(得分:4)

您误解了function listen () { console.log('coucou'); } 的功能。它返回一个对象,而不是单个字段。您需要首先引用该字段,然后将其分配给您的对象。

似乎还可以将两个查询合并为一个,因此下面是为您重构的代码库:

fetch_object()