mysql VS mysqli
// mysql
$link = mysql_connect('host mysql', 'ชื่อ', 'รหัส');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ชื่อฐานข้อมูล",$link);
$result = mysql_query('SELECT * FROM table WHERE 1=1');
$row = mysql_fetch_array($result);
echo mysql_error();
echo 'Connected successfully';
mysql_close($link);
// mysqli แบบProcedural style
$link = mysqli_connect('host mysql', 'ชื่อ', 'รหัส', 'ชื่อฐานข้อมูล');
if (!$link) {
die('Could not connect: ' . mysqli_connect_errno());
}
$result = mysqli_query($link, "SELECT * FROM table WHERE 1=1");
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
echo 'Connected successfully';
mysqli_close($link);
// mysqli แบบ OOP
$link = new mysqli('host mysql', 'ชื่อ', 'รหัส', 'ชื่อฐานข้อมูล');
if ($link->connect_error) {
die('Could not connect: ' . mysqli_connect_errno());
}
$result = $link->query($link, "SELECT * FROM table WHERE 1=1");
$row = $link->fetch_array($result,MYSQLI_ASSOC);
echo 'Connected successfully';
$link->close();
$link = mysql_connect('host mysql', 'ชื่อ', 'รหัส');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ชื่อฐานข้อมูล",$link);
$result = mysql_query('SELECT * FROM table WHERE 1=1');
$row = mysql_fetch_array($result);
echo mysql_error();
echo 'Connected successfully';
mysql_close($link);
// mysqli แบบProcedural style
$link = mysqli_connect('host mysql', 'ชื่อ', 'รหัส', 'ชื่อฐานข้อมูล');
if (!$link) {
die('Could not connect: ' . mysqli_connect_errno());
}
$result = mysqli_query($link, "SELECT * FROM table WHERE 1=1");
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
echo 'Connected successfully';
mysqli_close($link);
// mysqli แบบ OOP
$link = new mysqli('host mysql', 'ชื่อ', 'รหัส', 'ชื่อฐานข้อมูล');
if ($link->connect_error) {
die('Could not connect: ' . mysqli_connect_errno());
}
$result = $link->query($link, "SELECT * FROM table WHERE 1=1");
$row = $link->fetch_array($result,MYSQLI_ASSOC);
echo 'Connected successfully';
$link->close();
Comments
Post a Comment