简体   繁体   English

为什么我的代码出现PHP致命错误?

[英]Why am I getting a PHP Fatal error with my code?

I have been wracking my brain for hours now trying to figure out why my code is giving me the error: 我一直在动脑筋几个小时,试图弄清楚为什么我的代码给了我错误:

PHP Fatal error:  Call to a member function close() on a non-object in /var/www/Garage/ajaxFunctions/ServiceReplication.php on line 24

Here is my code. 这是我的代码。 It is the back end of an AJAX call in my application: 它是我的应用程序中AJAX调用的后端:

<?php
    /*
        Description:
        Parameters:
        Return:
    */
    include_once('../includes/config.inc.php');
    include_once('../includes/dbConnection.php');

    // $newTargetID = (isset($_GET['newTargetID']) && $_GET['newTargetID'] != '') ? strip_tags($_GET['newTargetID']) : null;
    // $service = (isset($_GET['service']) && $_GET['service'] != '') ? strip_tags($_GET['service']) : null;

    $newTargetID = "12988";
    $service = "16468";

    $returnValue = "0";

    if($newTargetID != null && $service != null){
        $ServiceReplicationQuery = "call ServiceReplication(" . $newTargetID . "," . $service . ")";
        error_log("ServiceReplicationQuery: " . $ServiceReplicationQuery);
        if($result = $dbConnection->query($ServiceReplicationQuery)){
            error_log("Successfully replicated service " . $service . " to target " . $newTargetID . ".");
            $returnValue = "1";
            $result->close();
        }
        else{
            error_log("Failure replicating service " . $service . " to target " . $newTargetID . ".");
        }
    }
    else{
        error_log("Failure replicating service " . $service . " to target " . $newTargetID . ".");
    }

    echo $returnValue;
?>

Note that the dbConnection.php include does have a valid and working mysqli db connection, and I have other AJAX back ends that are almost identical to this that work perfectly, the only difference is that I am using a stored procedure with this piece of code. 请注意,dbConnection.php include确实具有有效且有效的mysqli db连接,并且我还有其他AJAX后端,它们与该后端几乎完全相同,可以完美地工作,唯一的区别是,我在这段代码中使用存储过程。

I can take the error_log echo of my query, place it in MySQL Workbench, and it works fine. 我可以接受查询的error_log回显,并将其放在MySQL Workbench中,并且工作正常。 In fact, even though I get this error, the database is updated. 实际上,即使出现此错误,数据库也会被更新。 I am just getting this error and not getting the return value I need, which is "1". 我只是遇到此错误,而没有得到我需要的返回值,即“ 1”。

You are erroneously setting a variable instead of comparing it in an if statement: 您错误地设置了变量,而不是在if语句中进行比较:

if ($result = $dbConnection->query($ServiceReplicationQuery)) {

This will always evaluate to true. 这将始终为true。 The error you get is because you try to call method close() on $result, which is fatal when $result is not an object. 您收到的错误是因为您尝试在$ result上调用close()方法,这在$ result不是对象时是致命的。

Update your code to this instead of $result->close() : 更新您的代码,而不是$result->close()

$dbConnection->close();

Or if you really want to call close on $result: 或者,如果您真的想在$ result上调用close:

$result = $dbConnection->query($ServiceReplicationQuery)
if ($result) {

or this: 或这个:

$result = $dbConnection->query($ServiceReplicationQuery)
if (is_object($result)) {

depending on what is more appropriate in what you are trying to accomplish. 取决于您要完成的工作中哪个更合适。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM