PHP mysqli real_connect()ฟังก์ชั่น
ตัวอย่าง - สไตล์เชิงวัตถุ
เปิดการเชื่อมต่อใหม่ไปยังเซิร์ฟเวอร์ MySQL พร้อมตัวเลือกการเชื่อมต่อเพิ่มเติม:
<?php
$mysqli = mysqli_init();
if (!$mysqli) {
die("mysqli_init failed");
}
// Specify connection timeout
$con -> options(MYSQLI_OPT_CONNECT_TIMEOUT, 10);
// Specify read options from named file instead of my.cnf
$con ->
options(MYSQLI_READ_DEFAULT_FILE, "myfile.cnf");
$con -> real_connect("localhost","my_user","my_password","my_db");
?>
ดูตัวอย่างรูปแบบขั้นตอนที่ด้านล่าง
ความหมายและการใช้งาน
ฟังก์ชัน real_connect() / mysqli_real_connect() เปิดการเชื่อมต่อใหม่ไปยังเซิร์ฟเวอร์ MySQL
ฟังก์ชันนี้แตกต่างจากconnect()ในลักษณะต่อไปนี้:
- real_connect() ต้องการวัตถุที่ถูกต้องที่สร้างโดย init()
- real_connect() สามารถใช้กับ options() เพื่อตั้งค่าตัวเลือกต่างๆ สำหรับการเชื่อมต่อ
- real_connect() มีแฟล็กพารามิเตอร์
ไวยากรณ์
สไตล์เชิงวัตถุ:
$mysqli ->
real_connect(host, username, password, dbname, port, socket, flag)
รูปแบบขั้นตอน:
mysqli_real_connect(connection, host, username, password, dbname, port, socket, flag)
ค่าพารามิเตอร์
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
host | Optional. Specifies a host name or an IP address |
username | Optional. Specifies the MySQL username |
password | Optional. Specifies the MySQL password |
dbname | Optional. Specifies the default database to be used |
port | Optional. Specifies the port number to attempt to connect to the MySQL server |
socket | Optional. Specifies the socket or named pipe to be used |
flag | Optional. Specifies different connection options. Possible values:
|
รายละเอียดทางเทคนิค
คืนมูลค่า: | จริงกับความสำเร็จ FALSE เมื่อล้มเหลว |
---|---|
รุ่น PHP: | 5+ |
บันทึกการเปลี่ยนแปลง PHP: | PHP 5.6: เพิ่มการตั้งค่าสถานะ MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT |
ตัวอย่าง - รูปแบบขั้นตอน
เปิดการเชื่อมต่อใหม่ไปยังเซิร์ฟเวอร์ MySQL พร้อมตัวเลือกการเชื่อมต่อเพิ่มเติม:
<?php
$con = mysqli_init();
if (!$con) {
die("mysqli_init failed");
}
// Specify connection timeout
mysqli_options($con,
MYSQLI_OPT_CONNECT_TIMEOUT, 10);
// Specify read options from named file instead of my.cnf
mysqli_options($con, MYSQLI_READ_DEFAULT_FILE, "myfile.cnf");
mysqli_real_connect($con,"localhost","my_user","my_password","my_db");
?>
❮ PHP MySQLi อ้างอิง