mysqli_connect

(PHP 5, PHP 7)

mysqli_connectAlias of mysqli::__construct()

Description

This function is an alias of: mysqli::__construct()

Although the mysqli::__construct() documentation also includes procedural examples that use the mysqli_connect() function, here is a short example:

Examples

Example #1 mysqli_connect() example

<?php
$link 
mysqli_connect("127.0.0.1""my_user""my_password""my_db");

if (!
$link) {
    echo 
"Error: Unable to connect to MySQL." PHP_EOL;
    echo 
"Debugging errno: " mysqli_connect_errno() . PHP_EOL;
    echo 
"Debugging error: " mysqli_connect_error() . PHP_EOL;
    exit;
}

echo 
"Success: A proper connection to MySQL was made! The my_db database is great." PHP_EOL;
echo 
"Host information: " mysqli_get_host_info($link) . PHP_EOL;

mysqli_close($link);
?>

The above examples will output:

Success: A proper connection to MySQL was made! The my_db database is great.
Host information: localhost via TCP/IP
add a note add a note

User Contributed Notes 5 notes

up
2
zubkov dot rabota at gmail dot com
2 months ago
To set a permanent connection use prefix 'p:'
example:

"$pMysqli = new mysqli('p:'.DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);"
up
-8
thamizh82 at gmail dot com
8 months ago
$con=mysqli_connect("localhost","my_user","my_password","my_db");

var_dump($con);  -> Return mysql object.

$conn = mysqli_connect(NULL, $user, $pass);
$conn = mysqli_connect('NULL', $user, $pass);
Return Same object given before. Not thrown false or error.

$conn = mysqli_connect('NULLTest', $user, $pass);
Return false
up
-5
php dot net dot comments at siteaboutnothing dot com
2 months ago
By default port 3306 will be used.

To use a different port, enter it as another argument, not as part of the hostname:

mysqli_connect($db_host,$db_user,$db_pass,$db_database,$db_port);
up
-59
d4v3m0nk
11 months ago
Worthwhile noting that the MySQLi library can still open a permanent connection, but a different command isn't used. Simply prepend "p:" to the MySQL hostname or address.

Using classes:
$this->mysqli = new mysqli('p:'. mysql_host, mysql_user, mysql_password, mysql_database);

Using command:
$link = mysqli_connect('p:mysql_host', 'mysql_user', 'mysql_password', 'mysql_database');
up
-78
tuxedobob
11 months ago
When using this function rather than the OO syntax, this function will return FALSE on error, rather than an object.
To Top