Jump to content

Problem connecting to MtSQL db using a simple php script


larghifra

Recommended Posts

Hi everyone, i'm a new user and i'm making some test on 000webhost to host a database for a simple application.

I created a MySQL database that contains a simple table you can see here:

post-202991-0-76729700-1480435476_thumb.png

I made a simple android app which sends a Json request to this simple php file loaded into "public_html" folder in my 000webhost page.

 

<?php
    $conn = mysqli_connect("localhost", "myuser", "mypassword", "mydatabase");
    
    $name = $_POST["name"];
    $email = $_POST["email"];
    $password = $_POST["password"];
 
    $statement = mysqli_prepare($conn, "INSERT INTO user (name, email, password) VALUES (?, ?, ?)");
    mysqli_stmt_bind_param($statement, "siss", $name, $email, $password);
    mysqli_stmt_execute($statement);
    
    $response = array();
    $response["success"] = true;  
    
    echo json_encode($response);
?>
 
The application works correctly and receives the "success" response back equals to "true", so the php script runs correctly too, but in the database it doesn't happen anything.  :confused:
Maybe i made some error in the php script, i don't know, please help me!!  :(
 

 

Link to comment
Share on other sites

your query is failing, because you are listing the wrong number of inputs in the type string for the bind_param() statement. this would either be throwing a (php?) error at the bind_param() statement or a mysql error at the execute() statement. you need to ALWAYS detect and handle statement errors.

 

the easiest way of detecting and handling database statement errors is to use exceptions. to enable exceptions for the php msyqli extension, add the following two lines before you make the database connection -  

$driver = new mysqli_driver(); // note the $driver variable name used here is separate from and not related to any variable your code may be using
$driver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT; // MYSQLI_REPORT_ALL <-- w/index checking; w/o index checking --> MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT;
then, php will catch the exception when there is an error and if you have php's error_reporting set to E_ALL and display_errors set to ON, php will display the actual cause of the error and some back-trace information.
Link to comment
Share on other sites

 

your query is failing, because you are listing the wrong number of inputs in the type string for the bind_param() statement. this would either be throwing a (php?) error at the bind_param() statement or a mysql error at the execute() statement. you need to ALWAYS detect and handle statement errors.

 

the easiest way of detecting and handling database statement errors is to use exceptions. to enable exceptions for the php msyqli extension, add the following two lines before you make the database connection -  

$driver = new mysqli_driver(); // note the $driver variable name used here is separate from and not related to any variable your code may be using
$driver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT; // MYSQLI_REPORT_ALL <-- w/index checking; w/o index checking --> MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT;
then, php will catch the exception when there is an error and if you have php's error_reporting set to E_ALL and display_errors set to ON, php will display the actual cause of the error and some back-trace information.

 

 

Thank you for your answer! Sorry can you explain me where i can set the error reporting and display errors?

Edited by larghifra
Link to comment
Share on other sites

Thank you for your answer! Sorry can you explain me where i can set the error reporting and display errors?

 

Did you change the call to mysqli_stmt_bind_param() as suggested by Barand? If not, try changing this

mysqli_stmt_bind_param($statement, "siss", $name, $email, $password);

To this

mysqli_stmt_bind_param($statement, "sss", $name, $email, $password);

The other thing that Barand mentioned refers to this portion

$response = array();
$response["success"] = true;  
 
echo json_encode($response);

No matter what happens with the query, you will get "true" since it's hard coded. Instead, you need to test the return value of the call to mysqli_stmt_execute(). More information can be found here:

http://php.net/manual/en/mysqli-stmt.execute.php

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.