วันพุธที่ 9 กันยายน พ.ศ. 2552

Function mysql_connect

Mysql_connection Version ที่รองรับ (4,5)

Mysql_connection ใช้สำหรับเปิด Connection ไปที่ Mysqlserver

รูปแบบ

resource mysql_connect ([ string $server = ini_get("mysql.default_host") [, string $username = ini_get("mysql.default_user") [, string $password = ini_get("mysql.default_password") [, bool $new_link = false [, int $client_flags = 0 ]]]]] )

Parameters

  • servername คือ ชื่อของ server ที่มี mysql ที่ webserver มองเห็น ซึ่งอาจจะเป็น IP หรือ HostName ก็ได้
  • username คือ ชื่อผู้ใช้ ที่สามารถ Connection ไปยัง mysql Server ได้
  • password คือ รหัสผ่านที่ สามารถ Connection ไปยัง mysql Server ได้
  • new_link เป็น optionnal คือมีหรือไม่มีก็ได้ ซึ่งค่า Defaul ก็คือ false หมายถึงว่า เป็นการสร้าง Conneciton ใหม่ หรือเปล่า
  • client_flags เป็น optional คือ มีหรือไม่มีก็ได้ ซึ่งค่า Default ก็คือ 0

Return

  • ถ้าสำเร็จจะ return เป็น link_id เพื่อใช้ในการอ้างอิงถึง Connection ในการทำงานต่อๆ ไปถ้า ไม่สำเร็จจะ return false

Examples

<?php

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); // เปิด Connection ที่ localhost ด้วย username ชื่อ mysql_user และ password ชื่อ mysql_password
if (!$link) { // เช็คว่า ตัวแปร link มีค่า false หรือเปล่า โดยเครื่องหมาย ! คือ inverse ค่า
die('Could not connect: ' . mysql_error()); // ให้จบการทำงาน โดยพิมพ์ Could not connect พร้อมกับ ใช้ Function ในการแสดง Error ของ mysql
}
echo 'Connected successfully'; // แสดง text ว่า Connect สำเร็จ
mysql_close($link); // เป็น Function ในการ ปิด Connection โดย รับ parameter เป็น link_id ที่ได้จากการสร้าง Connection

?>