Can't Connect my php to mysql

Hello, I was testing to connect my php to mysql database and I’m struggling to connect my config.php and registration.php to the db. It also said that I successfully connected to the database but when I tried to enter the firstname, lastname, etc and checked on the phpmyadmin it doesn’t show there.

Note: I just put “password” on db_pass but actually what I put was the mysql pass, as well as for the db_name.

here’s the snippet from my codes:

Config.php:

<?php

$db_user = "epiz_32062790";

$db_host = "sql100.epizy.com";

$db_pass = "password";

$db_name = "epiz_*number*_*database_name*";

$charset = "utf8mb4";

// Create Connection

$conn = mysqli_connect($db_user, $db_host, $db_pass, $db_name);

//Check Connection

if ($conn->connect_error) {

die ("Connection failed: " . $conn->connect_error);

}

else {

echo "Connected Successfully";

}

?>

registration.php:

<?php
require_once ("config.php");
    if(isset($_POST['create'])){
        /*echo 'User submitted.';*/
        $Firstname           = $_POST['Firstname'];
        $Lastname            = $_POST['Lastname'];
        $Username            = $_POST['Username'];
        $Email               = $_POST['Email'];
        $Password            = $_POST['Password'];
        $ConfirmPassword     = $_POST['ConfirmPassword'];


            $sql = "INSERT INTO users (Firstname, Lastname, Username, Email, Password ) VALUES(?,?,?,?,?)";
       
       //Statement for insert, prepare and
        $stmtinsert = $conn->prepare($sql);
        $result = $stmtinsert->execute([$Firstname, $Lastname, $Username, $Email, $Password]);
        if($result){
            echo 'Successfully Saved';
        }
        else{
            echo 'ERROR: There was a problem while saving the data.';
        }

    }

?>

Can you screenshot the empty database for me?

Also, never put plaintext passwords into a database, if you get hacked, your users will hate you. Make sure they are encrypted (sha256 is good).

3 Likes

Here’s the screenshot from my database:

also here is where I’m testing the registration form for php and mysql database connection:
http://www.bagovanity.great-site.net/registration.php

Well you have the email column set as an integer, that will case an error. And since you are limiting the username to 16 characters, make sure your users know that.

2 Likes

Okay noted, I already changed the email column from integer to Varchar and refreshed the registration page and entered whatever required to do and it still did not recorded to the database.

Looking again, you also have a “ComfirmPassword” column that cannot be null. Since you are not submitting a value to that column, the database is rejecting it.

2 Likes

I have deleted/dropped the “ConfirmPassword” column, but upon testing it again it’s still not working or registering to the database.

Can you turn on PHP errors? This is under the PHP section in the control panel.

If that does not show you something useful, please share your URL.

3 Likes

I don’t see the PHP error setting nor PHP section on my Control Panel

And here’s my URL: http://www.bagovanity.great-site.net/registration.php

The setting is under “Alter PHP Config”.

3 Likes

I checked the form and after clicking sign up, the resulting page had a status code 500, which means it’s definitely a PHP code crash. display_errors should help a lot.

4 Likes

Oh i see the error now: The error was:

Warning : mysqli_connect(): (HY000/2002): Connection timed out in /home/vol14_1/epizy.com/epiz_32062790/htdocs/config.php on line 11

here’s on that config.php:

<?php

$db_user = "epiz_32062790";

$db_host = "sql100.epizy.com";

$db_pass = "";

$db_name = "epiz_32062790_bagovanity";

$charset = "utf8mb4";

// Create Connection

$conn = mysqli_connect($db_user, $db_host, $db_pass, $db_name);

//Check Connection

if ($conn->connect_error) {

die ("Connection failed: " . $conn->connect_error);


}

else {

echo "Connected Successfully";

}

?>

I can’t figure out how to fix the error (I’m still a newbie with connecting php to database).

I think I see the issue.

In your code, you do this:

$conn = mysqli_connect($db_user, $db_host, $db_pass, $db_name);

But according to the docs, the function is called like this:

mysqli_connect(
    string $hostname = ini_get("mysqli.default_host"),
    string $username = ini_get("mysqli.default_user"),
    string $password = ini_get("mysqli.default_pw"),
    string $database = "",
    int $port = ini_get("mysqli.default_port"),
    string $socket = ini_get("mysqli.default_socket")
): mysqli|false

The first argument is the host, the second is the user. You seem to be inserting them in the reverse order.

Finally, I checked the live file and it seems the database password is missing, and you don’t seem to be doing anything with the $charset param. But you should really call mysqli_set_charset as well because the defaults on our server are not that good.

4 Likes

I copied and change my code in config.php, it didn’t get any fatal error but it did get a syntax/parse error from the docs u sent:

Parse error : syntax error, unexpected ‘$db_host’ (T_VARIABLE), expecting ‘)’ in /home/vol14_1/epizy.com/epiz_32062790/htdocs/config.php on line 4

here’s the initial code:

<?php
// Create Connection
$conn = mysqli_connect(
    string $db_host = ini_get("sql100.epizy.com"),
    string $db_user = ini_get("epiz_32062790r"),
    string $db_pass = ini_get("password"),
    string $db_name = "epiz_32062790_bagovanity",
    int $db_port = ini_get("3306"),
   // string $socket = ini_get("mysqli.default_socket")
): mysqli|false

//$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die(“Could not create socket\n”);

/* You should enable error reporting for mysqli before attempting to make a connection */
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

/* Set the desired charset after establishing a connection */
mysqli_set_charset($conn, 'utf8mb4');

//Check Connection
if ($conn->connect_error) {
    die ("Connection failed: " . $conn->connect_error);
    }
else {
echo "Connected Successfully";
}

?>

You don’t understand. ini_get() gets default values from the PHP configuration. Instead of using ini_get(), just put the information in quotes, like they are in the parameter. Like this:

$conn = mysqli_connect(
    "sql100.epizy.com",
    "epiz_32062790",
    "<password>",
    "epiz_32062790_bagovanity"
);

And later, you are trying to use $conn object-oriented-ly, and that doesn’t work when you established the connection procedurally. Instead, do it like so:

if (mysqli_connect_error($conn)) {
    die("MySQL Error: " . mysqli_connect_error($conn));
}
5 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.