定義和用法
這個mysqli_real_connect()函數建立與 MySQL 服務器的連接,並將連接作為對象返回。 mysql_connect() 函數與 this 之間的區別在於它接受由 mysqli_real_connect() 創建的對象需要一個必須由函數 mysqli_init() 函數創建的有效對象,並且您可以使用 mysqli_options() 函數為連接設置附加選項。
用法
mysqli_real_connect($con,[$host, $username, $passwd, $dname, $port, $socket, $flags] )
參數
Sr.No | 參數及說明 |
---|---|
1 |
con(Optional) 這是一個表示與 MySQL 服務器的連接的對象。 |
2 |
host(Optional) 這表示主機名或 IP 地址。如果您將 Null 或 localhost 作為值傳遞給此參數,則本地主機被視為主機。 |
3 |
username(Optional) 這表示 MySQL 中的用戶名。 |
4 |
passwd(Optional) 這代表給定用戶的密碼。 |
5 |
dname(Optional) 這表示應在其中執行查詢的默認數據庫。 |
6 |
port(Optional) 這表示您要建立與 MySQL 服務器的連接的端口號。 |
7 |
socket(Optional) 這表示要使用的套接字。 |
8 |
flags(Optional) 代表不同連接選項的整數值,這可以是以下常量之一 -
|
返回值
此函數返回布爾值,如果連接成功則返回 true,如果連接失敗則返回 false。
PHP版本
這個函數最初是在 PHP 版本 5 中引入的,適用於所有後續版本。
示例
以下示例演示了 mysqli_real_connect() 函數的用法(程序風格) -
<?php
$db = mysqli_init();
//Creating the connection
$con = mysqli_real_connect($db, "localhost","root","password","test");
if($con){
print("Connection Established Successfully");
}else{
print("Connection Failed ");
}
?>
這將產生以下結果 -
Connection Established Successfully
示例
在麵向對象風格中,這個函數的語法是 $con-> real_connect();以下是麵向對象樣式 $minus 中此函數的示例;
<?php
$db = mysqli_init();
//Connecting to the database
$con = $db->real_connect("localhost","root","password","test");
if($con){
print("Connection Established Successfully");
}else{
print("Connection Failed ");
}
?>
這將產生以下結果 -
Connection Established Successfully
示例
<?php
$connection_mysql = mysqli_init();
if (!$connection_mysql){
die("mysqli_init failed");
}
if (!mysqli_real_connect($connection_mysql,"localhost","root","password","mydb")){
die("Connect Error:" . mysqli_connect_error());
}else{
echo "Connection was successful";
}
mysqli_close($connection_mysql);
?>
這將產生以下結果 -
Connection was successful
相關用法
- PHP mysqli_real_escape_string()用法及代碼示例
- PHP mysqli_real_query()用法及代碼示例
- PHP mysqli_refresh()用法及代碼示例
- PHP mysqli_rollback()用法及代碼示例
- PHP mysqli_get_server_info()用法及代碼示例
- PHP mysqli_data_seek()用法及代碼示例
- PHP mysqli_insert_id()用法及代碼示例
- PHP mysqli_fetch_assoc()用法及代碼示例
- PHP mysqli_connect_error()用法及代碼示例
- PHP mysqli_fetch_all()用法及代碼示例
- PHP mysqli_next_result()用法及代碼示例
- PHP mysqli_stmt_affected_rows()用法及代碼示例
- PHP mysqli_begin_transaction()用法及代碼示例
- PHP mysqli_fetch_lengths()用法及代碼示例
- PHP mysqli_character_set_name()用法及代碼示例
- PHP mysqli_free_result()用法及代碼示例
- PHP mysqli_fetch_row()用法及代碼示例
- PHP mysqli_debug()用法及代碼示例
- PHP mysqli_commit()用法及代碼示例
- PHP mysqli_fetch_field()用法及代碼示例
注:本文由純淨天空篩選整理自 PHP mysqli_real_connect() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。