skip to Main Content

When I execute SQL in $stmt from phpmyadmin, it works fine and I get the desired result. However, from PHP it is going in the exception code block. Also, mysqli_error or mysqli_errno is blank.

Here is my PHP code:

<?php

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

$fp = file_put_contents('1.log', "1 - Started rn");

function processMessage($update) {

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

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

        $fp = file_put_contents('1.log', $update["result"]["parameters"]["plannumber"]."rn", 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."rn", FILE_APPEND);

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

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

        if (!$result) {

            $errno = mysqli_errno($connection);

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

        }

        $data = mysqli_fetch_assoc($result);

        $count = $data['reccount'];

        $fp = file_put_contents('1.log', "Count=".$count."rn", 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. "rn", 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."rn");
    processMessage($update);
}

mysqli_close($connection);

?>

2

Answers


  1. Chosen as BEST ANSWER

    I brought the entire connection thing inside processMessage function (given below) and it still doesn't work. Statement is getting logged correctly in log file but $fp = file_put_contents('1.log', "Result=".$result."rn", FILE_APPEND); is not in log file.

        $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. "rn", FILE_APPEND);
            exit();
        }
    
        $stmt = "select count(*) AS reccount from plandetails where plannumber = '$planNo'";
    
        $fp = file_put_contents('1.log', "Statement=".$stmt."rn", FILE_APPEND);
    
        $result = mysqli_query($connection,$stmt);
    
        $fp = file_put_contents('1.log', "Result=".$result."rn", FILE_APPEND);
    

  2. inside the function processMessage, what is $connection’s value? its not a global variable and you are not passing it into the function as parameter

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search