Stuck at DB_connect();

I’m trying to insert some data into my Database and it seems that the code gets stuck at the connection part
code :
<?php

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

//Creating Array for JSON response
$response = array();


// Check if we got the field from the user
if (isset($_GET['temp']) && isset($_GET['hum'])) {
 
    $temp = $_GET['temp'];
    $hum = $_GET['hum'];
 
    // Include data base connect class
    $filepath = realpath (dirname(__FILE__));
	require_once($filepath."/db_connect.php");

 // Connecting to database 
    $db = new DB_CONNECT();
   //  $this->connect();
 echo "Mark 1"; 

    // Fire SQL query to insert data in weather
    $result = mysql_query("INSERT INTO weather(temp,hum) VALUES('$temp','$hum')");
    // Check for succesfull execution of query
    if ($result) {
        // successfully inserted 
        $response["success"] = 1;
        $response["message"] = "Weather successfully created.";
 
        // Show JSON response
        echo json_encode($response);
    } else {
        // Failed to insert data in database
        $response["success"] = 0;
        $response["message"] = "Something has been wrong";
 
        // Show JSON response
        echo json_encode($response);
    }
} else {
    // If required parameter is missing
    $response["success"] = 0;
    $response["message"] = "Parameter(s) are missing. Please check the request";
 
    // Show JSON response
    echo json_encode($response);
}
 $this->close();
?>

then when using http://multiprotogw.000webhostapp.com/iot/insert.php?temp=30&hum=32
for example “Mark 1” doesn’t appear and the codes gets stuck at DB_CONNECT();
thanks
?>

Error handling please, on fail, let it echo the error

1 Like

I’m sorry , i don’t have a lot of experience
Can you explain more please ?

Should be something like

if(!$this -> connect()){
    exit($this -> error());
}

This is supposed to be the connect.php code :slight_smile:

 <?php

class DB_CONNECT {
 
    // Constructor
    function __construct() {
        // Trying to connect to the database
        $this->connect();
    }
 
    // Destructor
    function __destruct() {
        // Closing the connection to database
        $this->close();
    }
 
   // Function to connect to the database
    function connect() {

        //importing dbconfig.php file which contains database credentials 
        $filepath = realpath (dirname(__FILE__));

        require_once($filepath."/dbconfig.php");
        
		// Connecting to mysql (phpmyadmin) database
        $con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());
 
        // Selecing database
        $db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());
 
        // returing connection cursor
        return $con;
    }
 
	// Function to close the database
    function close() {
        // Closing data base connection
        mysql_close();
    }
 
}
 
?>

I think mysql_error() and mysql_close() require a parameter? Like $con