-1

So I have a project of mine which we had been testing in a server using Google Cloud Platform. However, there is a bug where my API endpoint is not returning anything from an AJAX get request when a specific query string id is specified (does not happen in localhost). Here is the code, I am also aware about the sql injection risks so bear with me.

PHP Endpoint Script:

<?php
    header("Content-Type: application/json");
    include_once __DIR__.'/../helpers/mysql.php';
    include_once __DIR__.'/../helpers/helper.php';

    $helper = new Helper();
    $db = new Mysql_Driver();

    if (isset($_GET["module_id"]))
    {
        $module_id = $_GET["module_id"];
        $db->connect();
        $query = "SELECT m.*, i.item_path FROM Module m INNER JOIN Item i ON m.module_id = i.module_id WHERE m.module_id=$module_id";
        $moduleInfoResult = $db->query($query);
        // $moduleInfo = $db->fetch_array($moduleInfoResult);

        if ($db->num_rows($moduleInfoResult) > 0) {
            $moduleInfoResult = $db->fetch_array($moduleInfoResult);
            $response = array(
                'status' => 200,
                'message' => 'Success',
                'data' => $moduleInfoResult
            );
        } else {
            $response = array(
                'status' => 404,
                'message' => 'Query Failed: Query return 0 records'
            );
        }
    } else {
        $response = array(
            'status' => 400,
            'message' => 'Error Occured: Must provide module_id query string'
        );
    }

    echo json_encode($response)
?>

Screenshots of result using postman (id that is working): enter image description here

Screenshots of result using postman (id that is not working but works in localhost): enter image description here

What I have tried I tried echo'ing out the response and by using Postman I realized that ids that are returning nothing has this character:

enter image description here

Is there a way to prevent or escape this character?

jmsandiegoo
  • 413
  • 4
  • 14
  • 1
    So you basically have a problem with character encoding somewhere then? https://stackoverflow.com/questions/279170/utf-8-all-the-way-through – 04FS Jan 28 '20 at 12:19

1 Answers1

1

After a lot of research and debugging, I found out that the problem was character encoding! So what I did to solve the problem was to UTF-8 my mysql db and mysqli Reference link: Stack Overflow Thread Credits to @04FS

jmsandiegoo
  • 413
  • 4
  • 14