PHP mysqli_select_db() 函式



定義和用法

mysqli_select_db() 函式接受一個表示現有資料庫的字串值,並將其設定為預設資料庫。

語法

mysqli_select_db($con, name)

引數

序號 引數及描述
1

con(必填)

這是一個表示與 MySQL 伺服器連線的物件。

2

name(必填)

這是一個字串值,表示您需要設定為預設資料庫的現有資料庫的名稱。

返回值

PHP mysqli_select_db() 函式返回一個布林值,如果操作成功則為 true,否則為 false

PHP 版本

此函式首次引入於 PHP 5 版本,並在所有後續版本中均有效。

示例

以下示例演示了 mysqli_select_db() 函式(在過程式風格中)的用法 -

<?php
   //Creating a connection
   $con = mysqli_connect("localhost", "root", "password", "mydb");

   //Selecting the database
   mysqli_query($con, "CREATE DATABASE NewDatabase");
   mysqli_select_db($con, "NewDatabase");

   //Retrieving the current database name
   $res = mysqli_query($con, "SELECT DATABASE()");

   while ($row = mysqli_fetch_row($res)) {
      print("Current Database: ".$row[0]);
   }

   //Closing the connection
   mysqli_close($con);
?>

這將產生以下結果 -

Current Database: newdatabase

示例

在面向物件風格中,此函式的語法為 $con->select_db(); 以下是此函式在面向物件風格中的示例 $minus;

<?php
   //Creating a connection
   $con = new mysqli("localhost", "root", "password", "mydb");

   //Retrieving the current database name
   $res = $con->query("SELECT DATABASE()");
   while ($row = $res->fetch_row()) {
      print("Initial Database: ".$row[0]."\n");
   }

   //Selecting the database
   $con->query("CREATE DATABASE NewDatabase");
   $con->select_db("NewDatabase");

   //Retrieving the current database name
   $res = $con->query("SELECT DATABASE()");

   while ($row = $res->fetch_row()) {
      print("Current Database: ".$row[0]);
   }

   //Closing the connection
   $res = $con -> close();
?>

這將產生以下結果 -

Initial Database: mydb
Current Database: newdatabase

示例

除了在連線時指定資料庫外,您還可以使用此函式稍後選擇它,如下所示 -

<?php
   //Creating a connection
   $con = mysqli_connect("localhost", "root", "password");

   //Selecting the database
   mysqli_select_db($con, "mydb");
   print("Database Selected ..."."\n");

   mysqli_query($con, "CREATE TABLE IF NOT EXISTS my_team(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))");
   print("Table Created ..."."\n");

   //Inserting a records into the my_team table
   mysqli_query($con, "insert into my_team values(1, 'Shikhar', 'Dhawan', 'Delhi', 'India')");
   mysqli_query($con, "insert into my_team values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')");
   mysqli_query($con, "insert into my_team values(3, 'Kumara', 'Sangakkara', 'Matale', 'Srilanka')");
   mysqli_query($con, "insert into my_team values(4, 'Virat', 'Kohli', 'Delhi', 'India')");

   print("Records Inserted ..."."\n");
 
   //Closing the connection
   mysqli_close($con);
?>

這將產生以下結果 -

Database Selected ...
Table Created ...
Records Inserted ...

示例

<?php
   $connection_mysql = mysqli_connect("localhost", "root", "password","mydb");
   
   if (mysqli_connect_errno($connection_mysql)){
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }
   
   $res = mysqli_select_db($connection_mysql,"testdb");
   
   if($res){
	   echo "Database Selected";
   }else{
	   echo "Error Occurred";
   }
   
   mysqli_close($connection_mysql);

?>

這將產生以下結果 -

Database Selected
php_function_reference.htm
廣告