从PHP执行我的SQL选择时出错

时间:2019-01-30 20:15:17

标签: php mysql

当我从phpmyadmin在$ stmt中执行SQL时,它运行良好,并且得到了所需的结果。但是,从PHP进入异常代码块。另外,mysqli_errormysqli_errno为空白。

这是我的PHP代码:

<?php

header('Content-Type: application/json');

$fp = file_put_contents('1.log', "1 - Started \r\n");

function processMessage($update) {

    $fp = file_put_contents('1.log', "Inside processMessage \r\n", FILE_APPEND);

    if($update["result"]["action"] == ""){

        $fp = file_put_contents('1.log', $update["result"]["parameters"]["plannumber"]."\r\n", FILE_APPEND );

        $planNo = $update["result"]["parameters"]["plannumber"];

        $stmt = "select count(*) AS reccount from plandetails where plannumber = '$planNo'";

        $fp = file_put_contents('1.log', "Statement=".$stmt."\r\n", FILE_APPEND);

        $result = mysqli_query($connection,$stmt);

        $fp = file_put_contents('1.log', "Result=".$result."\r\n", FILE_APPEND);

        if (!$result) {

            $errno = mysqli_errno($connection);

            $fp = file_put_contents('1.log',"Error in Select Query: ". $errno . "\r\n", FILE_APPEND);
            exit;

        }

        $data = mysqli_fetch_assoc($result);

        $count = $data['reccount'];

        $fp = file_put_contents('1.log', "Count=".$count."\r\n", FILE_APPEND);

        If ($count > 0) {
            $speech = 'What is your Member ID';
        }
        else {
            $speech = 'Plan Number $planNo not found';
        }

        $fp = file_put_contents('1.log', "Speech=".$speech, FILE_APPEND);

            sendMessage(array(
                "source" => $update["result"]["source"],
                //"speech" => $update["result"]["parameters"]["plannumber"],
                "speech" => $speech,
                "displayText" => "........Text Here...........",
                "contextOut" => array()
            ));
    }

}

function sendMessage($parameters) {
    $req_dump = print_r($parameters, true);
    $fp = file_put_contents( 'Response.log', $req_dump);
    header('Content-Type: application/json');
    echo json_encode($parameters);
}

//open DB connection
    $host = "localhost";
    $username = "XXXX";
    $password = "XXXX";
    $database = "XXXX";

    $connection = mysqli_connect ($host, $username, $password, $database);

    if (mysqli_connect_errno()) {
        $con_errno = mysqli_connect_error();
        file_put_contents('1.log',"Error in DB Connection: " . $con_errno. "\r\n", FILE_APPEND);
        exit();
    }

$update_response = file_get_contents("php://input");
$fp = file_put_contents('input.log', $update_response);

$update = json_decode($update_response, true);

if (isset($update["result"]["action"])) {
    $fp = file_put_contents('1.log', "Inside isset:".$update."\r\n");
    processMessage($update);
}

mysqli_close($connection);

?>

2 个答案:

答案 0 :(得分:0)

在processMessage函数内部,$ connection的值是多少?它不是全局变量,并且您没有将其作为参数传递给函数

答案 1 :(得分:0)

我将整个连接内容带入了processMessage函数(如下所示)中,但仍然无法正常工作。语句已正确记录到日志文件中,但$ fp = file_put_contents('1.log',“ Result =”。$ result。“ \ r \ n”,FILE_APPEND);不在日志文件中。

    $planNo = $update["result"]["parameters"]["plannumber"];

    //open DB connection
    $host = "localhost";
    $username = "xxxx";
    $password = "xxxx";
    $database = "xxxx";

    $connection = mysqli_connect ($host, $username, $password, $database);

    if (mysqli_connect_errno()) {
        $con_errno = mysqli_connect_error();
        file_put_contents('1.log',"Error in DB Connection: " . $con_errno. "\r\n", FILE_APPEND);
        exit();
    }

    $stmt = "select count(*) AS reccount from plandetails where plannumber = '$planNo'";

    $fp = file_put_contents('1.log', "Statement=".$stmt."\r\n", FILE_APPEND);

    $result = mysqli_query($connection,$stmt);

    $fp = file_put_contents('1.log', "Result=".$result."\r\n", FILE_APPEND);