1

I have the code below whereby when I run it and provide parameters, the query fails to execute. The problem though is that when I try to find out what went wrong using mysqli_error($conn), it returns null. This is my PHP script:


------database.php-------

<?php
    
    function getConnection(){
        $conn = new mysqli('127.0.0.1','afrojobs_admin','Angelsdie123','afrojobs_fsid_water_billing_db');
        mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

        if(!$conn){
            Die('Fatal Error: '.mysqli_error($conn));
        }else{
            return $conn;
        }
    }

-----update.php-------

<?php
header('Access-Control-Allow-Origin:*');
$res = array();
if(isset($_GET['meter_number']) || !empty($_GET['meter_number'])
    || isset($_GET['url_link']) || !empty($_GET['url_link']) ){

    $_link = $_GET['url_link'];
    $meter_number = $_GET['meter_number'];

    require_once('database.php');
    $conn = getConnection();

    $sql = "UPDATE ssh_details SET url_link = ? WHERE meter_number= ?";
    // echo $sql; return;
    $stmt = mysqli_prepare($conn,$sql);
    mysqli_stmt_bind_param($stmt,'si',$_link,$meter_number);
    mysqli_stmt_execute($stmt);

    if($stmt){
        $res['status'] = 'success';
        $res['message'] = 'SSH Details Updated!';
        $res['code'] = 150;
    }else{
        $res['status'] = 'error';
        $res['message'] = mysqli_error($conn); 
        $res['code'] = 767;
    }
}else{
    $res['status'] = 'error';
    $res['message'] = 'Malformed request parameters!';
    $res['code'] = 403;
}

echo json_encode($res);

And this is my response:

{"status":"error","message":null,"code":767}

Why am I not able to get the error?

CliffTheCoder
  • 394
  • 1
  • 4
  • 24
  • 1
    Because you are using the wrong function. **You should never expose errors to the user. This is dangerous.** – Dharman Aug 03 '20 at 18:03
  • 1
    True, but this is simply a microservice of a larger application operation on a server-to-server communication. The errors are for debuggin and logging – CliffTheCoder Aug 03 '20 at 18:10
  • Errors should be logged by the application on the server not passed from one application to another. – Dharman Aug 03 '20 at 18:11

1 Answers1

-1

mysqli_error($conn) will not give you any errors when a statement fails. You would have to use mysqli_stmt_error($stmt) instead.

Warning! Never expose errors to the user. The error reporting for mysqli is switched off on purpose by default so that these errors are never shown to the user. You should enable error reporting once you make sure that your server is configured to never expose these messages to the user. Use this line before new mysqli() to enable error reporting.

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

For proper guidelines on handling DB errors properly see this post: mysqli_fetch_assoc() expects parameter / Call to a member function bind_param() errors. How to get the actual mysql error and fix it?

Dharman
  • 30,962
  • 25
  • 85
  • 135