定义和用法
这个mysqli_select_db()函数接受一个表示现有数据库的字符串值,并将其作为默认数据库。
用法
mysqli_select_db($con, name)
参数
Sr.No | 参数及说明 |
---|---|
1 |
con(Mandatory) 这是一个表示与 MySQL 服务器的连接的对象。 |
2 |
name(Mandatory) 这是一个字符串值,表示您需要将其设为默认数据库的现有数据库的名称。 |
返回值
PHP mysqli_select_db() 函数返回一个布尔值,如果操作成功则为真,否则为假。
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 mysqli_set_charset()用法及代码示例
- PHP mysqli_stmt_affected_rows()用法及代码示例
- PHP mysqli_stmt_fetch()用法及代码示例
- PHP mysqli_stmt_data_seek()用法及代码示例
- PHP mysqli_stmt_free_result()用法及代码示例
- PHP mysqli_stmt_error()用法及代码示例
- PHP mysqli_stmt_field_count()用法及代码示例
- PHP mysqli_stmt_errno()用法及代码示例
- PHP mysqli_stmt_get_result()用法及代码示例
- PHP mysqli_stmt_init()用法及代码示例
- PHP mysqli_stmt_bind_result()用法及代码示例
- PHP mysqli_stat()用法及代码示例
- PHP mysqli_stmt_store_result()用法及代码示例
- PHP mysqli_stmt_reset()用法及代码示例
- PHP mysqli_stmt_attr_get()用法及代码示例
- PHP mysqli_stmt_param_count()用法及代码示例
- PHP mysqli_stmt_attr_set()用法及代码示例
- PHP mysqli_stmt_prepare()用法及代码示例
- PHP mysqli_stmt_sqlstate()用法及代码示例
- PHP mysqli_stmt_bind_param()用法及代码示例
注:本文由纯净天空筛选整理自 PHP mysqli_select_db() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。