简体   繁体   中英

database is not getting connected

I am not able to connect to my database. Here's my code could you please help me out.

<?php

error_reporting(E_ALL); 
ini_set('display_errors', 1);

define('DB_HOST', 'mysql.hostinger.in');
define('DB_NAME', 'u25*****41_hari');
define('DB_USERNAME', 'u25*****41_hari');
define('DB_PASSWORD', 'hariharan');

$link = mysqli_connect(DB_HOST, DB_USERNAME,    DB_PASSWORD);

if (!$link) {
     die('Could not connect line 9');
}

$DB_SELECT = mysqli_select_db(DB_NAME, $link);

if (!$DB_SELECT) {
        die('Could not connect line 15');
}

$valueone = $_POST['Name'];
$valuetwo = $_POST['Username'];
$valuethree = $_POST['Password'];
$valuefour = $_POST['Mobile_Number'];

$sqlone = "INSERT INTO Account (Name) VALUES ('$valueone')";
$sqltwo = "INSERT INTO Account (Username) VALUES ('$valuetwo')";
$sqlthree = "INSERT INTO Account (Password) VALUES ('$valuethree')";
$sqlfour = "INSERT INTO Account (Mobile_Number) VALUES ('$valuefour')";

if (!mysql_query($sqlone) || !mysql_query($sqltwo) || !mysql_query($sqlthree) || !mysql_query($sqlfour) || !mysql_query($sqlfive)) {
     die('Could not connect name line 33');
}


mysql_close();
?>

This is the error I get:

Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in /home/u256114841/public_html/login.php on line 17 Could not connect line 15

Foreword: Make sure that all your form element name attributes match the POST arrays and that your database uses the correct types for the data input and their lengths.


In mysqli, the connection comes first.

you have

$DB_SELECT = mysqli_select_db(DB_NAME, $link);

which should read as

$DB_SELECT = mysqli_select_db($link, DB_NAME);

Read the manual:

But... you would be better off using all 4 parameters all in one go:

$link = mysqli_connect(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);

while getting rid of $DB_SELECT = mysqli_select_db($link, DB_NAME);

However, you are mixing MySQL APIs with mysql_ functions.

Those different APIs do not intermix.

Consult the following on Stack:

You also did not post your HTML form to go with your POST arrays.

Make sure that your form does use a POST method and that your inputs all hold the same name attribute.

NB: $_POST['Name'] with name="name" will fail, if that is what your form element is named as and the same for all the other inputs. Those are case-sensitive.

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

// rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.

Additionally, your code is prone to an SQL injection. Use a prepared statement.

You don't need to use seperate queries like that.

Just add the columns and values all in one go.

$sqlone = "INSERT INTO Account (Name, Username, Password, Mobile_Number) 
            VALUES ('$valueone', '$valuetwo', '$valuethree', '$valuefour')";

Using mysqli_real_escape_string() for all your values.

Ie:

$valueone = mysqli_real_escape_string($link, $_POST['Name']);

and doing the same for the others.

I also noticed that you may be storing passwords in plain text. This is not recommended.

Use one of the following:

Other links:


Additional references:

The correct is

mysqli_select_db ( mysqli $link , string $dbname )

You are putting

mysqli_select_db ( string $dbname , mysqli $link )

Fix this and tell me if worked. For more info, see docs: http://php.net/manual/pt_BR/mysqli.select-db.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM