MySQLi - Real Connect

语法

bool mysqli_real_connect ( mysqli $link 
   [, string $host 
   [, string $username 
   [, string $passwd 
   [, string $dbname 
   [, int $port 
   [, string $socket 
   [, int $flags ]]]]]]] )

定义和用法

它打开与 mysql 服务器的连接。

示例

试试下面的例子 −

<?php
   $servername = "localhost:3306";
   $username = "root";
   $password = "";
   $dbname = "TUTORIALS";
   $conn = new mysqli($servername, $username, $password, $dbname);
   
   if (!$conn->real_connect($servername, $username, $password, $dbname)) {
      die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
   }
   echo 'Success... ' . mysqli_get_host_info($conn) . "
";
   
   $stmt = $conn->prepare("INSERT INTO tutorials_auto (id, name) VALUES (?, ?)");
   $stmt->bind_param("is", $id, $name);
   $id = "10";
   $name = "sai";
   $stmt->execute();
   echo "New records created successfully";
   
   $stmt->close();
   
   if ($result = $conn->query("SELECT name FROM tutorials_auto LIMIT 10")) {
      printf("Select returned %d rows.
", $result->num_rows);
      $result->close();
   }
   $conn->close();
?>

上述代码的示例输出应该是这样的 −

Success... localhost:3306 via TCP/IP New records created successfullySelect returned 3 rows.

mysqli_useful_functions.html