gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:33:31 27 4
gpt4 key购买 nike

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

<?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}

Desired Response 正是这种格式,但数字是错误的。它们是查询值,所以很抱歉我无法给出准确的输出。我试过对其中一些行进行类型转换,但没有成功。我真的不明白为什么我会在我的日志中收到这个 int 转换错误,所以对此的解释会很棒。第一个值应始终为 in,第二个和第三个值应为 float。

另外,我无法理解大多数 PHP 文档的解释,所以请不要引用它们。

最佳答案

您误解了 fetch_object() 的作用。它返回一个对象,而不是单个字段。您需要首先引用该字段,然后将其分配给您的对象。

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

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

关于php - 如何从查询中设置对象属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51634520/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com