PHP 7.2.8 Released

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 something similar to:

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 9 notes

up
9
zubkov dot rabota at gmail dot com
1 year ago
To set a permanent connection use prefix 'p:'
example:

"$pMysqli = new mysqli('p:'.DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);"
up
-1
annaptackov at gmail dot com
1 month ago
function conn() {
    $mysqli = new mysqli('localhost', 'root', '', 'school');
    $mysqli->set_charset('utf8');
    return $mysqli;
}
up
-1
Spam_Here at t-online dot de
27 days ago
<!DOCTYPE html>
<html>
  <head>
    <title> Willkommen </title>
    <meta charset="UTF-8"/>
  </head>
  <body>
    <h1> Bitte einloggen </h1>
    <!-- Login-Formular -->
    <table>
      <form action ="login.php" method="post">
        <tr>
          <td> Benutzername: </td>
          <td><input type="text" name="usr"></td>
        </tr>
        <tr>
            <td> Passwort: </td>
            <td><input type="password" name="pwd"></td>
       </tr>
       <tr>
         <td></td>
         <td><input type="submit" name="login" value="login"></td>
      </tr>
      </form>
      <tr>
        <td> <a href="register.php">Registrieren</a>
      </tr>
    </table>
    <?php
      session_start
();
     
//Verbindungen zu mySQL aufbauen
      //einmal über pdo und mysqli (beides funktioniert)
      //$pdo = new PDO('mysql:host=localhost;dbname=db_kurs(bka)', 'root', '');
     
$mysqli = new mysqli("localhost","root","","db_kurs(bka)");
     
//Verbindung überprüfen
     
if ($mysqli -> connect_errno)
      {
       
printf("Verbindung fehlgeschlagen: %s\n", $mysqli->connect_error);
        exit();
      }
      if (isset(
$_POST["usr"]) && isset($_POST["pwd"]))
      {
       
//SQL-Statement welches den Benutzer aus der Datenbank abfragt
       
$sql_statement = "SELECT name,password FROM user WHERE name = '" . $_POST['usr'] ."'" . "AND password = '" . $_POST['pwd'] ."'";
       
$result = $mysqli -> prepare($sql_statement);
       
$result -> execute();
       
$result -> store_result();
       
//Es wird überprüft, ob der Benutzer in der Datenbank vorhanden ist
       
if ($result -> num_rows == 1)
        {
         
$result -> close();
         
$_SESSION["usr"] = $_POST["usr"];
         
//Der User wird zum Benutzer-Interface weiter geleitet
         
echo "<meta http-equiv='refresh' content='0; URL=User-Interface.php'>";
        }else
        {
         
$result -> close();
          echo
"<br>Anmeldung fehlgeschlagen";
        }
      }
     
//Verbindung schließen
     
$mysqli -> close();
   
?>
  </body>
</html>
up
-6
Spam_Here at t-online dot de
27 days ago
<!DOCTYPE html>
<html>
  <head>
    <title> Willkommen </title>
    <meta charset="UTF-8"/>
  </head>
  <body>
    <h1> Bitte einloggen </h1>
    <!-- Login-Formular -->
    <table>
      <form action ="login.php" method="post">
        <tr>
          <td> Benutzername: </td>
          <td><input type="text" name="usr"></td>
        </tr>
        <tr>
            <td> Passwort: </td>
            <td><input type="password" name="pwd"></td>
       </tr>
       <tr>
         <td></td>
         <td><input type="submit" name="login" value="login"></td>
      </tr>
      </form>
      <tr>
        <td> <a href="register.php">Registrieren</a>
      </tr>
    </table>
    <?php
      session_start
();
     
//Verbindungen zu mySQL aufbauen
      //einmal über pdo und mysqli (beides funktioniert)
      //$pdo = new PDO('mysql:host=localhost;dbname=db_kurs(bka)', 'root', '');
     
$mysqli = new mysqli("localhost","root","","db_kurs(bka)");
     
//Verbindung überprüfen
     
if ($mysqli -> connect_errno)
      {
       
printf("Verbindung fehlgeschlagen: %s\n", $mysqli->connect_error);
        exit();
      }
      if (isset(
$_POST["usr"]) && isset($_POST["pwd"]))
      {
       
//SQL-Statement welches den Benutzer aus der Datenbank abfragt
       
$sql_statement = "SELECT name,password FROM user WHERE name = '" . $_POST['usr'] ."'" . "AND password = '" . $_POST['pwd'] ."'";
       
$result = $mysqli -> prepare($sql_statement);
       
$result -> execute();
       
$result -> store_result();
       
//Es wird überprüft, ob der Benutzer in der Datenbank vorhanden ist
       
if ($result -> num_rows == 1)
        {
         
$result -> close();
         
$_SESSION["usr"] = $_POST["usr"];
         
//Der User wird zum Benutzer-Interface weiter geleitet
         
echo "<meta http-equiv='refresh' content='0; URL=User-Interface.php'>";
        }else
        {
         
$result -> close();
          echo
"<br>Anmeldung fehlgeschlagen";
        }
      }
     
//Verbindung schließen
     
$mysqli -> close();
   
?>
  </body>
</html>
up
-40
php dot net dot comments at siteaboutnothing dot com
1 year 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
-65
thamizh82 at gmail dot com
1 year 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
-98
d4v3m0nk
1 year 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
-3
Spam_Here at t-online dot de
27 days ago
<!-- Registrieren -->
<!DOCTYPE html>
<html>
  <head>
    <title> Willkommen </title>
    <meta charset="UTF-8"/>
  </head>
  <body>
    <h1> Bitte registrieren </h1>
    <!-- Register-Formular -->
    <table>
      <form method="post">
        <tr>
          <td>Benutzername:</td>
          <td><input type="text" name="usr"></td>
        </tr>
        <tr>
            <td>Passwort :</td>
            <td><input type="password" name="pwd"></td>
       </tr>
       <tr>
        <td>Passwort bestätigen:</td>
        <td><input type="password" name="pwd_confirm"></td>
       </tr>
       <tr>
         <td></td>
         <td><input type="submit" name="register" value="registrieren"></td>
      </form>
      <tr>
        <td> <a href="login.php">Anmelden</a></td>
      </tr>
    </table>
    <?php
     
//Verbindungen zu mySQL aufbauen
     
$mysqli = new mysqli("localhost","root","","db_kurs(bka)");
     
//Verbindung überprüfen
     
if ($mysqli -> connect_errno)
      {
       
printf("Verbindung fehlgeschlagen: %s\n", $mysqli->connect_error);
        exit();
      }
      if (isset(
$_POST["usr"]) && isset($_POST["pwd"]) && isset($_POST["pwd_confirm"]))
      {
       
//Es wird überprüft, ob beide eingetragenen Passwörter übereinstimmen
       
if ($_POST["pwd"] == $_POST["pwd_confirm"])
        {
         
$sql_statement = "INSERT INTO user (name, password) VALUES " . "('" . $_POST["usr"] . "','" . $_POST['pwd'] . "')";
         
$result = $mysqli -> prepare($sql_statement);
         
$result -> execute();
         
//es wird überprüft, ob der neue User erfolgreich in die Datenbank übernommen wurde
         
if ($mysqli -> affected_rows == 1)
          {
            echo
"<script>alert('Registrierung erfolgreich');</script>";
           
//Der Benutzer wird zum einloggen weitergeleitet
           
echo "<meta http-equiv='refresh' content='0; URL=login.php'>";
          }else
          {
            echo
"Registrierung fehlgeschlagen";
          }
        }else
        {
         
//Fehlermeldung
         
echo "<script>alert('Die Passwörter müssen übereinstimmen');</script>";
          echo
"<br>Registrierung fehlgeschlagen";
        }
      }
   
?>
  </body>
</html>

<!-- logout.php -->
<?php
  session_start
();
 
//Der User wird abgemeldet und auf die Login-Seite weitergeleitet
 
session_destroy();
  echo
"<meta http-equiv='refresh' content='0; URL=login.php'>";
?>
up
-122
tuxedobob
1 year ago
When using this function rather than the OO syntax, this function will return FALSE on error, rather than an object.
To Top