|
 |
|
เลิกใช้ฟังก์ชั่น mysql_xxxxx() ที่ล้าสมัย (deprecated) และเปลี่ยนมาใช้ MySQLi กันดีกว่า |
|
 |
|
|
 |
 |
|
การใช้ MySQLi แทนการใช้ฟังก์ชั่น mysql_xxxxx() นั้น เขียนได้ 2 แบบคือ
แบบ OOP (เรียกใช้ method ของ object)
กับแบบ Procedural (เรียกใช้ฟังก์ชั่น)
OOP $result = $mysqli->query("SELECT * FROM `table`");
Procedural $result = mysqli_query($mysqli, "SELECT * FROM `table`");
ในบทความนี้จะกล่าวถึงแบบ OOP เท่านั้น เพราะการเขียนแบบ OOP นั้นมีโอกาสที่จะเกิดความผิดพลาดน้อยกว่า
เหตุผลคือ การเขียน MySQLi แบบ Procedural จะต้องมีการส่งตัวแปร mysqli object ทุกครั้ง
พูดง่ายๆ คือต้องเขียนยาวกว่า และอาจจะสับสนได้ง่ายกว่า
และอาจจะทำงานช้ากว่า เพราะแบบหลังอาจจะเป็นแค่การเรียก method ของ object ที่ส่งไปในแบบ OOP นั่นเอง
ซึ่งจริงๆ แล้ว mysqli_query() อาจจะทำงานในลักษณะนี้ function mysqli_query(MySQLi $link, $query, $resultmode = MYSQLI_STORE_RESULT)
{
return $link->query($query, $resultmode);
}
การเชื่อมต่อฐานข้อมูลด้วย MySQLi
ก่อนจะใช้ MySQLi นั้น ก็เหมือนกับการใช้ฟังก์ชั่น mysql_xxxxx()
คือต้องเชื่อมต่อกับ MySQL Server ก่อน ซึ่งในแบบเดิมเราใช้ mysql_connect()
แต่ในการเขียนด้วย MySQLi จะทำได้สองแบบ
1. ด้วย MySQLi::__construct() หรือการ new mysqli()
แบบนี้จะสามารถสร้าง MySQLi Object และทำการเชื่อมต่อไปได้ในคำสั่งเดียว
$mysqli = new mysqli('localhost', 'root', 'password', 'default_db_name'); $mysqli = mysqli_connect('localhost', 'root', 'password', 'default_db_name');
2. สร้าง MySQLi Object ด้วย mysqli_init() และเชื่อมต่อด้วย MySQLi::real_connect()
โดย mysqli_init() จะสร้าง MySQLi Object ให้เพื่อเอาไปใช้กับ MySQLi::options() และ MySQLi::real_connect()
ความแตกต่างกับแบบแรกคือ แบบแรกจะไม่สามารถกำหนด options ให้กับการเชื่อมต่อ
เช่น ระยะเวลา timeout, คำสั่ง SQL เริ่มต้น, ตั้งค่าการเชื่อมต่อแบบ SSL ฯลฯ
$mysqli = mysqli_init();
// การปรับแต่ง options ต้องทำก่อนการเชื่อมต่อ
$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 3600);
// เชื่อมต่อ
$mysqli->real_connect('localhost', 'root', 'password', 'default_db_name');
ดังนั้นในส่วนของการเชื่อมต่อ เราใช้ method/function ดังนี้
MySQLi::__construct()
mysqli_connect()
สร้าง MySQLi Object และเชื่อมต่อฐานข้อมูลไปในคราวเดียว
MySQLi::__construct($host, $username, $password, $dbname)
mysqli_connect($host, $username, $password, $dbname) $host ชื่อหรือ IP ของ MySQL Server เช่น 'localhost' หรือ '127.0.0.1'
$username ชื่อผู้ใช้
$password รหัสผ่าน
$dbname ชื่อฐานข้อมูล default ที่จะใช้กับทุกๆ query ที่อ่านค่าจากตารางโดยไม่เจาะจงฐานข้อมูล
ค่าที่คืนกลับมา คืน MySQLi Object กลับมาเสมอ ไม่ว่าผลการเชื่อมต่อจะเป็นอย่างไร
ซึ่งหากจะตรวจสอบว่าเชื่อมต่อสำเร็จหรือไม่ ต้องตรวจด้วย MySQLi::$connect_errno หรือ MySQLi::$connect_error
ตัวอย่าง $mysqli = new mysqli('localhost', 'root', 'password', 'default_db_name');
// หรือ $mysqli = mysqli_connect('localhost', 'root', 'password', 'default_db_name');
if ($mysqli->connect_errno) {
echo $mysqli->connect_error;
exit;
}
mysqli_init()
สร้าง MySQLi Object เพื่อใช้ในการตั้งค่าด้วย MySQLi::options() และเชื่อมต่อด้วย MySQLi::real_connect()
ค่าที่คืนกลับมา MySQLi Object
ตัวอย่าง $mysqli = mysqli_init();
MySQLi::real_connect()
เชื่อมต่อฐานข้อมูล โดยต้องเป็น MySQLi Object ที่สร้างขึ้นจาก mysqli_init() เท่านั้น
MySQLi::real_connect($host, $username, $password, $dbname) $host ชื่อหรือ IP ของ MySQL Server เช่น 'localhost' หรือ '127.0.0.1'
$username ชื่อผู้ใช้
$password รหัสผ่าน
$dbname ชื่อฐานข้อมูล default ที่จะใช้กับทุกๆ query ที่อ่านค่าจากตารางโดยไม่เจาะจงฐานข้อมูล
ค่าที่คืนกลับมา true หากเชื่อมต่อสำเร็จ
false หากการเชื่อมต่อเกิดความผิดพลาด โดยสามารถตรวจสอบสาเหตุของความผิดพลาดได้ด้วย MySQLi::$connect_error
ตัวอย่าง $mysqli = mysqli_init();
$mysqli->real_connect('localhost', 'root', 'password', 'default_db_name');
MySQLi::$connect_errno
MySQLi::$connect_error
เป็น property ที่บอกว่า การเรียกใช้ MySQLi::__construct() หรือ MySQLi::real_connect() นั้นมีความผิดพลาดเกิดขึ้นหรือไม่
โดย MySQLi::$connect_errno เป็นตัวเลขของ error code
ส่วน MySQLi::$connect_error จะเป็น error message
หากไม่มี error เกิดขึ้น MySQLi::$connect_errno จะมีค่าเป็น 0
ส่วน MySQLi::$connect_error จะมีค่าเป็น ''
ตัวอย่าง $mysqli = mysqli_init();
// ใช้ @ ปิดการแสดง warning ไว้ในกรณีที่เราอยากจะแสดง error message ในแบบของเราเอง
@$mysqli->real_connect('localhost', 'root', 'password', 'default_db_name');
if ($mysqli->connect_errno) {
echo $mysqli->connect_error;
exit;
}
ผลลัพธ์อาจจะเป็นเช่นนี้ Access denied for user 'root'@'localhost' (using password: YES)
การใช้คำสั่ง SQL
เมื่อเราได้ MySQLi Object จากการเชื่อมต่อฐานข้อมูลที่สำเร็จแล้ว
ก็มาถึงการใช้งาน MySQLi Object นั้นๆ ซึ่งโดยปกติก็คือการส่งคำสั่ง SQL ไปให้ MySQL Server ประมวลผล
ในการเขียนแบบเก่าเราใช้ฟังก์ชั่น mysql_query()
แต่ในแบบใหม่ เราจะใช้ method MySQLi::query() หรือ MySQLi::real_query() และอื่นๆ
MySQLi::query()
ส่งคำสั่ง SQL ไปให้ MySQL Server ประมวลผล
MySQLi::query($query) $query คำสั่ง SQL ไปให้ MySQL Server ประมวลผล ซึ่งควรจะ escape ด้วย MySQLi::real_escape_string()
ค่าที่คืนกลับมา false หากเกิดความผิดพลาด ซึ่งสามารถตรวจสอบสาเหตุของความผิดพลาดได้ด้วย MySQLi::$errno และ MySQLi::$error
true หากคำสั่ง SQL ที่ส่งไปเป็นคำสั่งที่ไม่คืนผลลัพธ์เป็นข้อมูลกลับมา เช่น INSERT, UPDATE, DELETE ฯลฯ
MySQLi_Result Object หากคำสั่ง SQL ที่ส่งไปเป็นคำสั่งที่อ่านข้อมูลกลับมา เ่ช่น SELECT, SHOW, EXPLAIN ฯลฯ
ตัวอย่าง การ INSERT // escape ค่าที่มาจากผู้ใช้เพื่อป้องกัน SQL Injection
$_POST['username'] = $mysqli->real_escape_string($_POST['username']);
$_POST['password'] = $mysqli->real_escape_string($_POST['password']);
// $result จะเ็ป็น Boolean true หรือ false บอกว่าการ query นั้นๆ สำเร็จหรือไม่
$result = $mysqli->query(
"
INSERT INTO `users`
(`username`, `password`)
VALUES
('$_POST[username]', '$_POST[password]')
"
); ตัวอย่างการ SELECT // escape ค่าที่มาจากผู้ใช้เพื่อป้องกัน SQL Injection
$_POST['id'] = $mysqli->real_escape_string($_POST['id']);
// $result จะเป็น instance ของคลาส MySQLi_Result
$result = $mysqli->query("SELECT * FROM `users` WHERE `id` = '$_POST[id]'");
// ซึ่งจะมี method ที่เกี่ยวข้องกับการดึงข้อมูล เทียบเท่ากับฟังก์ชั่นพวก mysql_fetch_xxxxx()
$row = $result->fetch_assoc();
// แสดงข้อมูลแถวแรก
echo $row['username'];
MySQLi_Result Object
เป็น Object ที่จะได้จากการเรียกใช้ method MySQLi::query() (และอื่นที่คล้ายคลึงกัน)
ซึ่งเป็น Object ที่มี method ในการตรวจสอบและดึงข้อมูล
กระบวนการ query และรับ result นี้ ผมขอเปรียบกับการสั่งซื้อของกับยี่ปั๊วทางโทรศัพท์
ซึ่งการ query คือการโทรไปสั่งซื้อ บอกว่าต้องการอะไร
และ result ที่ได้รับกลับคืนมาจากการ query นั้น "ยังไม่ใช่สินค้า"
แต่เป็น "รถขนสินค้าและพนักงานยกของ"
// เฮียๆ สั่งเบียร์หน่อย เอาสิงห์ กับช้างนะ รวมๆ กันมา 50 ลังล่ะกัน
$result = $mysqli->query(
"
SELECT * FROM `stock`
WHERE `type` = 'beer' AND `brand` IN ('Singha', 'Chang')
LIMIT 50
"
);
// เปรียบให้ $result คือรถขนสินค้า ที่ตอนนี้บรรทุกเบียร์ที่สั่งไปอยู่เต็มคันรถ
// ถามขนขับรถดูซิว่าขนมากี่ลัง ครบมั้ย
echo $result->num_rows;
// ให้พนักงานขนของช่วยขนเบียร์ลงมาดูสักลังก่อนซิ
$item = $result->fetch_assoc();
// โอเค ถูกต้อง ยกมาหมดเลยล่ะกัน
$warehouse = array();
// ขนลงมาเรื่อยๆ จนกว่าจะไม่มีของให้คนแล้ว
while (($item = $result->fetch_assoc())) {
// ยกเบียร์เข้าไปเก็บในโกดังทีละลัง
$warehouse[] = $item;
}
// บอกให้คนขับรถกลับไปได้แล้ว
$result->free();
MySQLi_Result::fetch_xxxxx()
เป็น method ที่ใช้ดึงข้อมูลที่ได้มาจากการ query ซึ่งมีหลาย method ด้วยกัน แตกต่างตามชนิดข้อมูลที่คืนมา
MySQLi_Result::fetch_assoc() ข้อมูลที่ได้จะเป็น associative array
MySQLi_Result::fetch_row() ข้อมูลที่ได้จะเป็น indexed array
MySQLi_Result::fetch_array() ข้อมูลที่ได้จะเป็นทั้ง indexed array และ associative array
MySQLi_Result::fetch_object() ข้อมูลที่ได้จะเป็น object
โดยปกติเราจะใช้ MySQLi_Result::fetch_assoc() เป็นหลัก
และไม่ควรใช้ MySQLi_Result::fetch_array() เพราะจะทำให้เปลืองหน่วยความจำโดยใช่เหตุ
ตัวอย่าง MySQLi_Result::fetch_assoc() $result = $mysqli->query("SELECT * FROM `games`");
$row = $result->fetch_assoc();
print_r($row);
// เข้าถึงด้วยชื่อคอลัมน์ในตาราง
echo $row['title']; ผลลัพธ์
Array
(
[id] => 1
[released] => 2012-09-20
[title] => Pro Evolution Soccer 2013
[price] => 1090
)
Pro Evolution Soccer 2013
ตัวอย่าง MySQLi_Result::fetch_row() $result = $mysqli->query("SELECT * FROM `games`");
$row = $result->fetch_row();
print_r($row);
// เข้าถึงข้อมูลด้วยลำดับของคอลัมน์ในตาราง
echo $row[2]; ผลลัพธ์
Array
(
[0] => 1
[1] => 2012-09-20
[2] => Pro Evolution Soccer 2013
[3] => 1090
)
Pro Evolution Soccer 2013
ตัวอย่าง MySQLi_Result::fetch_array() $result = $mysqli->query("SELECT * FROM `games`");
$row = $result->fetch_array();
print_r($row);
// เข้าถึงข้อมูลได้ทั้งสองแบบ
echo $row['2'];
echo $row['title']; ผลลัพธ์เป็น array ที่มี key ทั้งสองแบบ แต่ไม่ควรใช้เพราะเปลืองหน่วยความจำ (จะเห็นว่ามีข้อมูลที่ซ้ำกัน)
Array
(
[0] => 1
[id] => 1
[1] => 2012-09-20
[released] => 2012-09-20
[2] => Pro Evolution Soccer 2013
[title] => Pro Evolution Soccer 2013
[3] => 1090
[price] => 1090
)
Pro Evolution Soccer 2013Pro Evolution Soccer 2013
ตัวอย่าง MySQLi_Result::fetch_row() $result = $mysqli->query("SELECT * FROM `games`");
$row = $result->fetch_object();
print_r($row);
// ต้องเข้าถึงข้อมูลในแบบ Object
echo $row->title; ผลลัพธ์เป็นชนิด Object
stdClass Object
(
[id] => 1
[released] => 2012-09-20
[title] => Pro Evolution Soccer 2013
[price] => 1090
)
Pro Evolution Soccer 2013
MySQLi_Result::free()
ทำลายข้อมูลที่ query มาก่อนหน้า เพื่อคืนหน่วยความจำให้กับระบบ
ในการ query เพื่อให้ได้ MySQLi_Result Object นั้น ทุกๆ ครั้ง MySQL จะต้องเก็บผลลัพธ์ที่ได้ไว้ใน buffer ก่อน
ซึ่งอาจจะเป็นในหน่วยความจำ (RAM) หรือในฮาร์ดดิสก์
หลังจากนั้นจึงให้ผู้ใช้อ่านข้อมูลด้วย method fetch_xxxxx() ตามที่กล่าวมาข้างต้น
ซึ่ง buffer ที่ว่านี้จะยังคงอยู่ และยังคงกินทรัพยากรระบบ
จนกว่าจะมีการเรียก method นี้ หรือจนกว่าโปรแกรม PHP จะจบการทำงาน
ดังนั้นเราควรเรียกใช้ method นี้ทุกครั้งเมื่ออ่านข้อมูลออกมาจนครบ หรือไม่ต้องการอ่านข้อมูลจาก MySQLi_Result Object นั้นๆ แล้ว
ตัวอย่าง $result = $mysqli->query("SELECT * FROM `games`");
while (($row = $result->fetch_assoc())) {
// do something
}
$result->free();
MySQLi_Result::$num_rows
จำนวนแถวที่อยู่ในผลลัพธ์
ตัวอย่าง MySQLi_Result::fetch_row() $result = $mysqli->query("SELECT * FROM `games` LIMIT 10");
echo $result->num_rows; ผลลัพธ์ 10
อ่านบทความที่อาจจะมีการอัพเดทมากกว่าในกระทู้นี้ ที่นี่ ครับ
Tag : PHP, MySQL

|
ประวัติการแก้ไข 2013-06-04 11:14:30 2013-06-04 11:15:33
|
 |
 |
 |
 |
Date :
2013-06-02 11:41:47 |
By :
cookiephp |
View :
450813 |
Reply :
64 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
สุดยอดไปเลยค้าบ ช่วยผมในการเรียนรู้ OOP ได้เยอะจุงเบย 55^^
|
 |
 |
 |
 |
Date :
2013-06-02 12:56:05 |
By :
biggest59 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ช่วยลองใช้งานและวิจารณ์กันด้วยนะครับ หรือถ้ามี bug อะไรยังไงก็แจ้งเข้ามาได้
สำหรับคนอื่นจะดีหรือเปล่าผมไม่รู้ แต่ขอบอกเลยว่านี่ผมเผยแพร่สิ่งที่ผมใช้หากินเลยนะครับ
สำหรับผมแล้ว การเขียนคลาส helper แบบนี้ทำให้อะไรๆ ง่ายขึ้นเยอะ
โดยไม่ต้องไปพึ่งเฟรมเวิร์คใหญ่ๆ ที่จะทำให้โปรแกรมของเราช้าลงไปอีก หากใช้ไม่เป็น หรือไม่เข้าใจมันเพียงพอ
และมันก็เป็นอีกเหตุผลหนึ่งที่ผมปล่อยอันนี้มา เพราะเคยเห็นสมาชิกท่านหนึ่งเคยพูดประมาณว่า
ถ้าต้อง escape ตัวแปรบ่อยๆ เพื่อป้องกัน SQL Injection ขนาดนั้น คงต้องหาเฟรมเวิร์คมาใช้
ซึ่งผมคิดว่า มันไม่ใช่เหตุผล แก้ไขผิดจุดไปนิดนึงครับ
ถ้ามือใหม่ใช้คลาสนี้ หรือ helper ลักษณะนี้ ผมว่าเราคงตอบคำถามซ้ำๆ เดิมๆ กันน้อยลงครับ
อย่างน้อยก็ไม่ต้องมานั่งบอกว่า
"เอา sql มาดูครับ"
ป.ล. ถ้า TC Admin คิดว่าเป็นประโยชน์ ก็ช่วยปักหมุดให้หน่อยนะครับ
ป.ล.2 ยังมีอธิบายการใช้งานเพิ่มเติมนะครับ เกี่ยวกับคลาส MySQL_Result เด๋วจะมาเขียนต่อครับ
|
 |
 |
 |
 |
Date :
2013-06-02 13:09:52 |
By :
cookiephp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อ่อ แล้วก็คำอธิบายการใช้งาน ตรงไหนไม่เข้าใจ แจ้งเข้ามาได้เลยนะครับ
จะได้ปรับปรุง
|
 |
 |
 |
 |
Date :
2013-06-02 13:11:59 |
By :
cookiephp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
-อยากให้เพิ่ม method select_db ครับ เพราะผมว่าการตัดส่วนนี้ออกไปมันจะทำให้ใช้ยุ่งยากกว่าเดิมโดยไม่จำเป็น
-เมธอด query อยากให้มีการคืนค่า จะเป็น boolean หรือ exception ก็แล้วแต่ด้วยครับ เพราะบางครั้งการทำงานอานจไม่สำเร็จ เช่นเอา user ที่อ่านข้อมูลได้อย่าเดียวไปแก้ข้อมูล จะได้ใช้คำสั่งพวก OR die("555"); ได้อ่ะครับ
|
 |
 |
 |
 |
Date :
2013-06-02 13:21:33 |
By :
itpcc |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เออ ผมถามอะไรนิดนึงดิ
ถ้าเกิดการ query ที่ต้องใช้เครื่องหมายเปอร์เซ็นต์ พวก
Code (SQL)
SELECT * FROM test WHERE name LIKE %e%
แล้วจะแทรกเครื่องหมายยังไงอ่ะครับ เพราะเครื่องหมายนี้ถูกใช้เป็น identifier ไปแล้ว
|
 |
 |
 |
 |
Date :
2013-06-03 13:36:08 |
By :
itpcc |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
จะเป็นบทความหนึ่งที่สมาชิกจะอ่านเยอะมากครับ ผมสังเกตุว่าช่วงหลัง ๆ บทความที่เกี่ยวกับเทคนิคใหม่ ๆ ของ php สมาชิกจะแชร์กันเยอะมากครับ
|
 |
 |
 |
 |
Date :
2013-06-03 18:01:12 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอสลับเนื้อหานะครับ
คิดไปคิดมา การบังคับให้ใช้คลาสของตัวเอง จะดูจำกัดความรู้ไป
ขอให้ความรู้จริงๆ เพื่อให้ทุกคนได้เริ่มเข้าใจ และไปหาความรู้เพิ่มเติมได้ง่ายขึ้นครับ
|
 |
 |
 |
 |
Date :
2013-06-04 07:52:22 |
By :
cookiephp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
^^ รอ pdo
|
 |
 |
 |
 |
Date :
2013-06-04 08:44:00 |
By :
ห้ามตอบเกินวันละ 2 กระทู้ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช้มา 5 ปีแล้ว ของเขาดีจริงครับ 
ขอบคุณสำหรับบทความครับผม
|
 |
 |
 |
 |
Date :
2013-06-04 10:12:13 |
By :
Pine |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เยี่ยมมากครับ
|
 |
 |
 |
 |
Date :
2013-06-19 10:21:36 |
By :
kalamell |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เมื่อเร็ว ๆ นี้ ได้ข่าวมาว่า จะมีการยกเลิก การให้ใช้ฟรี mysql และเปลี่ยนมาใช้ mariaDb แทน ถ้าเป็นแบบนี้ เราจำเป็นต้องเตรียมตัวอย่างไรบ้างครับ
และมันจะมีผลกระทบต่อ DB ของเรายังงัยบ้างครับ
|
 |
 |
 |
 |
Date :
2013-06-23 23:51:04 |
By :
sescii |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
สอบถามนิดหนึ่งครับ คือผมลองใช้ mysqli เพื่อ connect db มันได้ผล ครับแต่มันอ่านภาษาไทยไม่ออก พอเปลี่ยนเป็น mysql ธรรมดามันถึงอ่านได้ แก้ไงครับ
|
 |
 |
 |
 |
Date :
2013-06-24 01:20:23 |
By :
cgckn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช้ mysqli::set_charset() ครับ
Code (PHP)
$mysqli->set_charset('utf8');
|
 |
 |
 |
 |
Date :
2013-06-27 13:58:53 |
By :
cookiephp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
t5
|
 |
 |
 |
 |
Date :
2013-07-19 15:47:20 |
By :
t5 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อย่างนี้ต้องศึกษา MariaDB ไว้แต่เนิ่นๆแล้วคับ
|
 |
 |
 |
 |
Date :
2013-07-27 21:50:38 |
By :
vinai55 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่ต้องตกใจกันไปครับ
เพราะ MariaDB คือ MySQL ครับ มันถูกสร้างมาเพื่อ "แทนที่" MySQL
ต่อให้ MySQL ต้องจ่ายตังค์ และทุกเซิร์ฟเวอร์เปลี่ยนเป็น MariaDB
คำสั่งอะไรต่อมิอะไรที่เราเคยใช้ใน PHP (และในภาษาอื่นๆ) ก็ยังสามารถใช้ได้กับ MariaDB ครับ
เพราะมัน API เดียวกัน ทางฝั่งคนเขียนโปรแกรมอาจจะไม่รู้เสียด้วยซ้ำว่า database server ที่รันอยู่เป็น MariaDB
อาจจะมีบาง feature ที่อาจจะใช้ไม่ได้ เพราะ MariaDB ไม่ support หรือมีเฉพาะใน MariaDB อันนั้นก็ว่ากันไป
แต่เท่าที่รู้ ณ เวลานี้ มีลักษณะน้อยมากครับ และไม่ใช่ feature หลักที่ใช้ๆ กันอยู่
|
 |
 |
 |
 |
Date :
2013-07-28 14:47:23 |
By :
cookiephp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แหล่มเลย ^^
|
 |
 |
 |
 |
Date :
2013-09-23 14:21:51 |
By :
StyleIndy |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เหมือน php oop เลย
|
 |
 |
 |
 |
Date :
2013-09-27 09:18:31 |
By :
verture |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แสดงว่าเรายังหล้าสมัยอยู่ ฮ่า ๆ ยังใช้แบบเก่าอยู่เลย ถึงเวลาเปลี่ยนแล้วซิ ขอบคุณครับ
|
 |
 |
 |
 |
Date :
2013-12-09 23:12:04 |
By :
charintr |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณ ครับ ที่ให้ ความรู้ใหม่ๆ
|
 |
 |
 |
 |
Date :
2014-01-05 13:20:38 |
By :
Wasukri |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ปัญหาคือ Server ที่ใช้ Upgrade หรือยังนี่สิ 555+ เรียนรู้ไว้ไม่เสียหลาย
|
ประวัติการแก้ไข 2014-01-26 01:33:30
 |
 |
 |
 |
Date :
2014-01-26 01:32:57 |
By :
dexjanghan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
$result = $mysqli->query("SELECT * FROM `table`");
|
 |
 |
 |
 |
Date :
2014-02-27 21:04:06 |
By :
dsadad |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (JavaScript)
$result = $mysqli->query("SELECT * FROM `table`");
|
 |
 |
 |
 |
Date :
2014-02-27 21:04:47 |
By :
sdad |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับ สำหรับความรู้
|
 |
 |
 |
 |
Date :
2014-02-28 05:22:25 |
By :
gravity99 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ได้ความรู้เพิ่มขึ้นอีก ขอบคุณมากๆ ค่ะ
|
 |
 |
 |
 |
Date :
2014-03-01 19:32:44 |
By :
jamong |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณความรู้ใหม่ๆครับ  
|
 |
 |
 |
 |
Date :
2014-03-08 20:32:31 |
By :
hackclub9 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เราใช้ PDO ก็โอเคดีนะ
|
 |
 |
 |
 |
Date :
2014-03-16 17:05:15 |
By :
phun |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณคับ
|
 |
 |
 |
 |
Date :
2014-03-26 23:10:52 |
By :
pratin |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับ เกือบไม่รอด 
|
 |
 |
 |
 |
Date :
2014-05-27 13:43:28 |
By :
teerapat_kan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ต้องตั้งใจศึกษา เหมือนที่ จขคถ ให้ความรู้หน่อยซ่ะแล้ว
ขอบคุณความรู้ใหม่ๆครับ

|
 |
 |
 |
 |
Date :
2014-06-24 10:54:33 |
By :
Zakariya |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณสำหรับความรู้ครับ
|
 |
 |
 |
 |
Date :
2014-07-13 15:08:59 |
By :
picpost |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
อ่านแล้วเข้าใจดีเลยครับขอบคุณครับ
|
 |
 |
 |
 |
Date :
2014-11-21 17:12:08 |
By :
SOUL |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ต้องลองเปลี่ยนไปตามโลกบ้างแล้วเน้อเรา 
|
 |
 |
 |
 |
Date :
2015-01-07 11:52:27 |
By :
boyeng3k |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ดีมากเลยครับใช้งานง่ายกว่าเดิ.ม
|
ประวัติการแก้ไข 2015-04-16 09:44:13 2015-04-16 09:45:32
 |
 |
 |
 |
Date :
2015-04-16 09:43:54 |
By :
Digitalhong |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณสำหรับความรู้ใหม่คะ 
|
 |
 |
 |
 |
Date :
2015-05-17 23:08:23 |
By :
JennifeR |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
สุดยอดดดด ขอบคุณนะครับ
|
 |
 |
 |
 |
Date :
2015-08-25 17:16:38 |
By :
NuItMaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถึง เจ้าของกระทู้
กระทู้นี้มีประโยชน์มาก แต่ว่า เนื่องจากวันเวลาและเหตุการณ์มันเปลี่ยนไปแล้ว
คำว่า mysql_xxx ที่ล้าสมัย มันไม่ใช่แล้ว มันกลายเป็นว่าเขาเอาออกยกเลิกใช้ไปเรียบร้อยแล้ว
เลยอยากแนะนำให้เจ้าของกระทู้เปลี่ยนหัวกระทู้ให้มันดูตื่นเต้น ดูเป็นเรื่องร้ายแรงมาก สักหน่อยน่ะครับ 
ปัจจุบันเห็นพวกเริ่มหัดทำและพวกไม่ยอมอัพเดทตัวเอง ยังเอามาใช้กันไม่หยุดไม่หย่อนเลย mysql_xxxx เนี่ย แล้วเดี๋ยวก็มีปัญหาต่ออีก เห็นแล้วเพลียแทน
|
 |
 |
 |
 |
Date :
2015-12-13 09:12:51 |
By :
mr.v |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
<?php
class clsConnect{
public $table = '';
public function __construct(){
$this->db = mysqli_init();
$this->db->options(MYSQLI_OPT_CONNECT_TIMEOUT, 3600);
$this->db->real_connect('localhost', 'root', 'root', 'test');
$this->db->set_charset('utf8');
}
public function SelectAll($table, $where=array(), $orderby='', $limit=''){
if(!empty($where)){
foreach((array)$where as $i => $item){
$item = $this->db->real_escape_string($item);
$qrywhere .= "$i = '$item' AND ";
}
}
if(!empty($orderby)){
$orderby = "ORDER BY $orderby";
}
if(!empty($limit)){
$limit = "LIMIT $limit";
}
$sql="
SELECT *
FROM $table
WHERE
$qrywhere
1
$orderby
$limit
";
$result = array();
$query = $this->db->query($sql);
$result['num'] = $query->num_rows;
while (($item = $query->fetch_assoc())) {
$result['row'][] = $item;
}
return $result;
$result->free();
}
public function SelectOne($table, $where=array(), $orderby=''){
if(!empty($where)){
foreach((array)$where as $i => $item){
$item = $this->db->real_escape_string($item);
$qrywhere .= "$i = '$item' AND ";
}
}
if(!empty($orderby)){
$orderby = "ORDER BY $orderby";
}
$sql="
SELECT *
FROM $table
WHERE
$qrywhere
1
$orderby
LIMIT 0,1
";
$result = array();
$query = $this->db->query($sql);
$result['row'] = $query->fetch_assoc();
$result['num'] = $query->num_rows;
return $result;
$result->free();
}
public function Insert($table='', $data=array()){
if(!empty($data)){
$attribute_arr = array();
$values_arr = array();
foreach($data as $fields => $val){
$attribute_arr[] = $fields;
$values_arr[] ="'".$this->db->real_escape_string($val)."'";
}
$attribute = implode(',', $attribute_arr);
$values = implode(',', $values_arr);
$sql="
INSERT INTO $table ($attribute)
VALUES($values);
";
$query = $this->db->query($sql);
if($query){
$result['success'] = 'OK';
$result['code'] = $this->db->insert_id;
}else{
$result['success'] = 'FAIL';
$result['error'] = $this->db->error;
}
}else{
$result['success'] = 'FAIL';
$result['error'] = 'NOT FOUND DATA';
}
return $result;
}
public function Update($table='', $data=array(), $where=array()){
if(!empty($data)){
$attribute_arr = array();
$where_arr = array();
foreach($data as $fields => $value){
$value = $this->db->real_escape_string($value);
$attribute_arr[] = " $fields = '$value' ";
}
foreach($where as $fields => $value){
$value = $this->db->real_escape_string($value);
$where_arr[] = " $fields = '$value' ";
}
$attribute = implode(', ', $attribute_arr);
$whereqry = implode(' AND ', $where_arr);
$sql="
UPDATE $table SET
$attribute
WHERE
$whereqry
";
$query = $this->db->query($sql);
if($query){
$result['success'] = 'OK';
}else{
$result['success'] = 'FAIL';
$result['error'] = $this->db->error;
}
}else{
$result['success'] = 'FAIL';
$result['error'] = 'NOT FOUND DATA';
}
return $result;
}
public function Delete($table='', $where=array()){
if(!empty($where)){
$where_arr = array();
foreach($where as $fields => $value){
$value = $this->db->real_escape_string($value);
$where_arr[] = " $fields = '$value' ";
}
$whereqry = implode(' AND ', $where_arr);
$sql="
DELETE FROM
$table
WHERE
$whereqry
";
$query = $this->db->query($sql);
if($query){
$result['success'] = 'OK';
}else{
$result['success'] = 'FAIL';
$result['error'] = $this->db->error;
}
}else{
$result['success'] = 'FAIL';
$this->error[] = 'NOT FOUND DATA';
}
return $result;
}
}
$db = new clsConnect();
?>
|
 |
 |
 |
 |
Date :
2016-06-22 17:43:21 |
By :
progamer2000 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมก็ใช้ MySQLi อยู่ครับ เพราะใน PHP7 จะไม่มี MySQL ธรรมดาแล้วครับ
ปล. ฝาก CMS ฟรี ไว้ในอ้อมใจด้วยครับ
http://www.cszcms.com

|
 |
 |
 |
 |
Date :
2016-07-01 11:08:21 |
By :
cskaza |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมว่าเปลี่ยนไปใช้ PDO ดีกว่าครับ
ผมว่ามันยืดหยุ่นกว่าเยอะเลย
|
 |
 |
 |
 |
Date :
2016-08-10 09:37:58 |
By :
peakna |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากครับ
|
 |
 |
 |
 |
Date :
2016-11-26 09:19:56 |
By :
NoomAllStar |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมาก ๆ ครับผม
|
 |
 |
 |
 |
Date :
2016-12-02 00:19:13 |
By :
อาเฟ้ย |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับผม
|
 |
 |
 |
 |
Date :
2017-01-09 17:31:03 |
By :
lotteryth |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

ขอบคุณมากเลยค่าา
|
 |
 |
 |
 |
Date :
2017-07-09 21:06:24 |
By :
kornbobo |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เนื่องจากว่าปัจจุบันยังมีคนใช้ฟังก์ชั่น mysql_xxxx() กันอยู่มาก ทั้งที่ทาง PHP ได้เลิกใช้มาเป็นสิบปีแล้ว
และการเปลี่ยนจาก Procedual มาเป็น Object (OOP) สำหรับผู้ที่อนุรักษ์นิยมมาก่อนหรือเอาโค้ดเก่าๆมาเล่นโดยไม่รู้ทัน การเปลี่ยนแบบนั้นมันเป็นเรื่องยากอยู่เหมือนกัน
ดังนั้นผมจึงได้ทดลองเขียนฟังก์ชั่น mysql_xxxx() เกือบทุกอย่างที่มีในเว็บ php.net เพื่อที่จะทดสอบรันบน PHP 5.5+
และผมยังได้พยายามเขียนฟังก์ชั่นที่ทาง PHP ได้แนะนำให้ไปใช้แทนด้วย เพื่อทดสอบว่ามันทำงานได้จริงบน PHP 5.5 ขึ้นไป (ทดสอบแล้วถึง 7.2).
ผู้ที่จะเปลี่ยนโดยใช้แบบเดิมไม่เอาแบบ Object ก็ขอให้ก๊อปโค้ดไปทดลองรันดูทั้งหมด, ปรับแต่งค่าฐานข้อมูลให้ถูกต้องเอาเอง สร้างฐานข้อมูลแล้ว import sql แล้วทดลองรันดูส่วนของไฟล์ที่ใช้ mysqli_xxxx() ไม่ควรจะมี error. เมื่อทดลองแล้วทำงานได้จริง การเปลี่ยนจากอะไรเป็นอะไร ขอให้ใช้เครื่องมือ compare files มาช่วยเพราะผมเขียนให้เทียบได้บรรทัดต่อบรรทัดเลย แล้วก็อ่าน comment ในโค้ดด้วยว่าอันไหนควรไปใช้อะไร.
dummy_people.sql
Code (SQL)
-- phpMyAdmin SQL Dump
-- version 3.3.10
-- http://www.phpmyadmin.net
--
-- Host: sql.test.dev
-- Generation Time: Oct 14, 2018 at 01:01 PM
-- Server version: 1.0.307
-- PHP Version: 5.5.38
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- Database: `test_generic-tests`
--
-- --------------------------------------------------------
--
-- Table structure for table `dummy_people`
--
DROP TABLE IF EXISTS `dummy_people`;
CREATE TABLE IF NOT EXISTS `dummy_people` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) DEFAULT NULL,
`address` varchar(255) DEFAULT NULL,
`add` date DEFAULT NULL,
`update` date DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=101 ;
--
-- Dumping data for table `dummy_people`
--
INSERT INTO `dummy_people` (`id`, `name`, `address`, `add`, `update`) VALUES
(1, 'Madyson Cummerata', '332 Jast Knoll\nEast Carolanne, WA 70869-1568', '1981-03-06', '2017-10-14'),
(2, 'Mrs. Kathlyn Olson', '', '2000-12-16', '2005-09-06'),
(3, 'Freeda Hodkiewicz', '23132 Kovacek Alley Apt. 060\nHowellfort, NJ 47945', '1996-02-12', '2006-01-07'),
(4, 'Shanny Weissnat', '63976 Huels Inlet\nSouth Noemiehaven, CA 60696-8648', '2009-10-01', '2017-06-10'),
(5, 'Demarcus Huels', '627 Reynolds Plain\nNorth Nicoport, MD 72711-7777', '1977-11-04', '1998-10-18'),
(6, 'Vivianne Ankunding', '', '1992-07-01', '2009-10-09'),
(7, 'Mrs. Tiffany Zboncak III', '16701 Kariane Trafficway\nWest Marvinborough, AR 31211-7884', '1970-02-18', '1992-12-11'),
(8, 'Prof. Aditya Marks DVM', '', '1973-08-20', '1979-07-09'),
(9, 'Demarcus Reichel', '336 Ulises Gateway\nLehnerfurt, MS 11457', '1976-06-10', '2002-07-22'),
(10, 'Dr. Clifford Pfannerstill III', '2171 Swift Street\nNew Elenora, NE 09377-3135', '1974-04-07', '1995-08-17'),
(11, 'Omer Jakubowski', '1599 Kozey Hollow\nNew Mortimer, DC 79489', '1970-05-25', '1985-10-30'),
(12, 'Pierce Goodwin', '', '1997-01-11', '2010-06-13'),
(13, 'Eric Stracke', '', '2007-04-22', '2013-05-01'),
(14, 'Prof. Lillian Gottlieb DDS', '', '1983-04-10', '1987-10-15'),
(15, 'Keely Wolf V', '', '1982-09-29', '1983-04-05'),
(16, 'Jenifer Hessel', '141 Abshire Cape Apt. 492\nEast Kaiahaven, NH 62861', '2006-01-19', '2013-03-28'),
(17, 'Ms. Annie Crist PhD', '164 Brianne Fork\nEthylmouth, RI 29274', '2004-07-09', '2017-10-30'),
(18, 'Julien Durgan II', '', '1977-11-16', '2005-10-19'),
(19, 'Fanny Medhurst', '5901 Hammes Knoll Suite 962\nNew Rodolfoville, WA 58497-2812', '2006-07-13', '2014-09-17'),
(20, 'Mr. Brady Nolan', '', '1991-10-06', '2009-10-08'),
(21, 'Prof. Edd Jenkins', '', '1973-05-16', '2007-04-12'),
(22, 'Dr. Zechariah Wehner II', '', '2005-06-21', '2008-09-03'),
(23, 'Dr. Otho Baumbach', '33183 Botsford Lights Suite 700\nPort Emory, NY 69796-4721', '1989-05-30', '2016-03-31'),
(24, 'Dr. Ken Nienow MD', '', '1999-08-06', '2004-10-19'),
(25, 'Angie Emard', '1304 Adam Stravenue\nNorth Josephine, IA 24726-4293', '1989-03-05', '2009-12-05'),
(26, 'Mrs. Clare Moore DVM', '30228 Hassan Parkways Suite 891\nFeeneyton, VA 49823-9406', '1995-02-13', '2012-02-21'),
(27, 'Chad Kihn', '4378 Hessel Pass\nColestad, DE 62671', '1988-07-28', '1999-01-24'),
(28, 'Lillian Abernathy', '', '1986-12-03', '1989-11-20'),
(29, 'Jermaine Mertz', '0404 Christiansen Plain\nMelissaland, MO 89100-0022', '1986-07-30', '1986-08-28'),
(30, 'Karley Kiehn', '64088 West Pine\nReynoldsville, MS 53962-0094', '2009-06-08', '2010-11-13'),
(31, 'Rick Purdy Sr.', '', '1971-11-16', '2014-01-02'),
(32, 'Mrs. Dixie Zulauf', '34071 Hilpert Crest\nSouth Verda, CT 65833', '2013-07-13', '2017-06-04'),
(33, 'Mr. Houston Becker Sr.', '', '2011-03-28', '2012-12-27'),
(34, 'Douglas Stoltenberg', '', '1978-11-10', '1989-11-27'),
(35, 'Miss Euna Bogisich MD', '03292 Spinka Well Apt. 811\nWatsicaport, NJ 26446', '2001-05-13', '2007-02-04'),
(36, 'Shanna Will', '', '1989-05-28', '2001-12-22'),
(37, 'Maci Tremblay', '', '1974-01-08', '1992-03-08'),
(38, 'Kale Little', '145 Michelle Villages\nNikolausshire, DC 90899-4237', '2004-08-13', '2009-07-16'),
(39, 'Prof. Marjory Bernhard', '807 Kaya Shore Suite 024\nWeimannland, MN 87429', '2000-05-14', '2015-08-16'),
(40, 'Mrs. Lilly Yost Sr.', '41050 Henri Overpass Suite 832\nNorth Norbert, AZ 69792', '1979-05-11', '2004-04-07'),
(41, 'Jermaine Ullrich', '780 Karli Plain Apt. 028\nNew Luisaview, NV 43642-9893', '1982-07-10', '2017-02-16'),
(42, 'Noe Waelchi', '286 Hermann Spurs\nMacejkovicland, OR 57023-1271', '1983-05-28', '2014-10-02'),
(43, 'Elton Leuschke', '131 Donnelly Springs Apt. 982\nBeattyport, AK 61774', '1993-05-21', '2017-04-10'),
(44, 'Sabina Ritchie', '', '1977-02-17', '1991-06-09'),
(45, 'Garland Franecki PhD', '8839 Eliezer View Suite 402\nRatkehaven, WI 37340-9306', '1977-12-18', '2011-10-26'),
(46, 'Elta Block', '2088 Naomie Bridge\nEvieburgh, KY 64789-9679', '1970-07-29', '2008-05-16'),
(47, 'Elliot Grimes', '10911 Block Pass Suite 188\nEast Cole, HI 74601-2351', '1983-05-19', '1992-09-12'),
(48, 'Bonnie Schamberger', '', '1976-03-13', '2008-05-26'),
(49, 'Clint Nikolaus', '', '1976-08-23', '1995-03-06'),
(50, 'Deborah Shanahan', '2849 Lennie Hills Apt. 149\nKulasport, ME 98881-8601', '1982-11-05', '1992-08-08'),
(51, 'Adaline Balistreri Sr.', '', '1979-03-02', '2006-07-14'),
(52, 'Dr. Maryjane Bashirian PhD', '6142 Gladys Knolls Apt. 388\nLake Oren, OK 90451', '1981-12-06', '1988-11-02'),
(53, 'Prof. Emile Towne', '1158 Predovic Mews\nNorth Reilly, UT 03140-3757', '1977-03-21', '2000-12-23'),
(54, 'Francesca Hyatt', '53428 Salvador Mountain Suite 037\nMcLaughlinfurt, IL 01812', '2006-04-05', '2006-09-20'),
(55, 'Napoleon Deckow', '4168 Kerluke Pass\nPort Trudie, SC 34117-3121', '1999-04-10', '2007-11-10'),
(56, 'Monica Schoen', '', '1979-07-22', '1993-11-12'),
(57, 'Jewel Denesik', '', '1989-01-04', '2012-07-11'),
(58, 'Elton Howe MD', '7818 Willie Land Apt. 853\nSouth Furmanview, KS 73233-7641', '1978-11-01', '1983-08-04'),
(59, 'Kiley Moore', '33801 Claire Garden Suite 671\nEast Gladyce, SD 00964', '1998-12-01', '2004-11-29'),
(60, 'Alexa Kassulke', '46725 Funk Unions Apt. 666\nNew Makenzieview, OH 07728-3272', '1974-06-08', '2013-01-06'),
(61, 'Makenna Wolf', '', '1970-07-06', '1985-10-12'),
(62, 'Dean Hudson', '452 Pfeffer Ports Apt. 179\nLake Brandt, AR 68063-5169', '1971-08-04', '1996-11-13'),
(63, 'Natalie Pollich', '2499 Simeon Ville\nPort Providencibury, GA 74416-9502', '2013-12-25', '2018-05-02'),
(64, 'Dr. Adolf Haley', '1549 Collins Expressway\nKeeblerborough, IA 45350', '1998-01-16', '2011-07-19'),
(65, 'Carlotta Haley', '97541 Salvatore Isle\nSouth Louvenia, TX 01461', '1974-01-20', '1976-11-09'),
(66, 'Lura Lockman', '094 Tavares Square\nPortertown, SC 15621-2305', '1970-06-29', '2016-12-31'),
(67, 'Ronny Schimmel', '', '1978-03-27', '2015-12-17'),
(68, 'Ottilie Gleichner', '', '1973-06-12', '1984-06-27'),
(69, 'Mr. Kennith Vandervort', '96841 Bode Road\nJeremyborough, OK 10772-0965', '1996-02-05', '2018-03-07'),
(70, 'Jayme Hoppe II', '', '1972-09-10', '2016-09-10'),
(71, 'Prof. Sigmund Fahey I', '', '1980-12-20', '2018-07-14'),
(72, 'Enrique Pacocha III', '17250 Williamson Locks\nLegrostown, CT 51718', '1972-07-24', '2000-11-02'),
(73, 'Mario Runte', '7329 Alexandre Union\nGuiseppefurt, AL 80690', '1987-07-15', '2005-07-29'),
(74, 'Arnaldo O''Reilly DDS', '275 Eldred Haven\nJastberg, NJ 09708', '1982-02-06', '2017-05-22'),
(75, 'Tess Considine V', '295 Lebsack Mill\nLarsonside, NV 51504', '1982-06-11', '2013-05-30'),
(76, 'Prof. Lennie Gusikowski V', '', '1974-06-27', '1976-11-19'),
(77, 'Crystal Boehm', '6173 Williamson Valleys Suite 030\nSouth Friedrichport, GA 42164', '2003-04-02', '2013-08-18'),
(78, 'Madison Heathcote', '', '1985-05-31', '1990-06-27'),
(79, 'Emmett Roberts', '9811 Lindgren Tunnel\nPort Jarret, NJ 81231-2814', '1988-09-12', '2002-04-28'),
(80, 'Elyse Reilly', '', '2005-04-13', '2009-12-29'),
(81, 'Prof. Dorcas Marks MD', '', '1976-03-04', '1990-06-28'),
(82, 'Dr. Paris Rau Sr.', '6575 Kovacek Common\nSouth Natasha, ND 02620', '1982-12-19', '2012-03-01'),
(83, 'Kellie Leuschke', '481 Maybelle Square Apt. 696\nNorth Marquiston, OK 35314-9684', '2003-02-09', '2003-07-06'),
(84, 'Austyn Bartell III', '', '1970-10-26', '1975-01-17'),
(85, 'Rogelio Murazik', '1379 Beatty Underpass\nKassulkeburgh, FL 29984-4782', '1970-06-03', '1977-04-25'),
(86, 'Tyrel Wilderman MD', '', '1994-11-19', '2002-12-24'),
(87, 'Hunter Yundt', '', '1970-05-30', '1977-07-22'),
(88, 'Stefanie Gottlieb', '906 Macie Pines\nPort Kieranstad, WV 99856-3185', '1975-05-17', '1976-10-22'),
(89, 'Melyssa Reilly', '', '2007-02-10', '2011-07-23'),
(90, 'Mona Weissnat Sr.', '', '1971-09-11', '1993-09-17'),
(91, 'Magdalen Dicki III', '', '1976-07-16', '2006-05-29'),
(92, 'Mr. Emiliano Wunsch III', '51071 Jabari Rapids Suite 739\nMaggioburgh, AK 08908', '1994-01-11', '2008-07-08'),
(93, 'Prof. Edwardo Little V', '5927 Elda Spurs\nWest Marleetown, HI 22866-8199', '1970-07-10', '1976-07-20'),
(94, 'Vito Windler', '', '1980-01-24', '1984-06-22'),
(95, 'Claudia Rodriguez', '', '1980-01-22', '2018-09-15'),
(96, 'Calista Shanahan', '', '2003-08-01', '2013-06-22'),
(97, 'Miss Katheryn Runolfsson MD', '2497 Akeem Passage Apt. 180\nFeltonstad, KS 25664-2828', '1981-10-10', '2017-10-16'),
(98, 'Don Breitenberg', '', '1976-02-27', '1986-12-28'),
(99, 'Weldon Cummerata', '5069 Sauer Villages Suite 730\nDooleyfort, NY 07181-5051', '1987-09-11', '1998-07-29'),
(100, 'Darlene Schoen', '1146 McClure Flat Suite 040\nNorth Alfonsomouth, LA 15781', '1981-12-15', '1988-11-19');
_config.php
Code (PHP)
<?php
$db_host = 'localhost';
$db_username = 'user';
$db_password = 'pass';
$db_name = 'test_generic-tests';
_functions.php
Code (PHP)
<?php
function printResult($functionName, $result)
{
echo '<p><code style="background-color: #f1f1f1; color: #e01d8b; padding: 2px 4px;">' . $functionName . '</code>: ';
if (is_scalar($result) || is_bool($result)) {
if (is_bool($result)) {
echo '<i style="background-color: #eee; padding: 2px 4px;">' . var_export($result, true) . '</i>';
} else {
echo '<i style="background-color: #eee; padding: 2px 4px;">' . $result . '</i>';
}
echo ' <span style="color: #999;">(' . gettype($result) . ')</span>';
echo '</p>' . PHP_EOL;
} else {
echo ' <span style="color: #999;">(' . gettype($result) . ')</span>';
echo '</p>' . PHP_EOL;
echo '<pre style="background-color: #555; color: #eee; margin: 0 0 40px; padding: 10px;">' . print_r($result, true) . '</pre>' . PHP_EOL;
}
echo PHP_EOL;
}// printResult
legacy-mysql-functions.php
Code (PHP)
<?php
require '_config.php';
require '_functions.php';
echo '<h1>Start testing mysql_xxx() functions</h1>' . PHP_EOL;
$link2 = mysql_pconnect($db_host, $db_username, $db_password);
printResult('mysql_pconnect', $link2);
if ($link2 === false) {
throw new \Exception('Could not connect to the database: ' . mysql_error());
}
mysql_close($link2);
unset($link2);
$link = mysql_connect($db_host, $db_username, $db_password);
printResult('mysql_connect', $link);
if ($link === false) {
throw new \Exception('Could not connect to the database: ' . mysql_error());
}
$db_selected = mysql_select_db($db_name);
printResult('mysql_select_db', $db_selected);
if ($db_selected === false) {
throw new \Exception('Could not select the database: ' . mysql_error());
}
mysql_query('UPDATE `dummy_people` SET `update` = \'' . date('Y-m-d') . '\' WHERE id=\'1\';');
printResult('mysql_affected_rows', mysql_affected_rows());
printResult('mysql_client_encoding', mysql_client_encoding());
// mysql_create_db() use `mysql_query('CREATE ...')` command instead.
$result = mysql_query('SELECT * FROM `dummy_people`');
printResult('mysql_data_seek', mysql_data_seek($result, 10));
mysql_free_result($result);
unset($result);
// mysql_db_name() use `mysql_query('SELECT DATABASE')` instead.
// mysql_db_query() use `mysql_query()` instead.
// mysql_drop_db() use `mysql_query('DROP DATABASE')` instead.
$result = mysql_query('SELECT * FROM `not_exists_table`');
printResult('mysql_errno', mysql_errno());
printResult('mysql_error', mysql_error());
unset($result);
// mysql_escape_string() use `mysql_real_escape_string()` instead.
$result = mysql_query('SELECT * FROM `dummy_people`');
printResult('mysql_fetch_array', mysql_fetch_array($result, MYSQL_NUM));
printResult('mysql_fetch_assoc', mysql_fetch_assoc($result));
printResult('mysql_fetch_field', mysql_fetch_field($result));
printResult('mysql_fetch_lengths', mysql_fetch_lengths($result));
printResult('mysql_fetch_object', mysql_fetch_object($result));
printResult('mysql_fetch_row', mysql_fetch_row($result));
printResult('mysql_field_flags', mysql_field_flags($result, 0));
printResult('mysql_field_len', mysql_field_len($result, 0));
printResult('mysql_field_name', mysql_field_name($result, 0));
printResult('mysql_field_seek', mysql_field_seek($result, 0));
printResult('mysql_field_table', mysql_field_table($result, 0));
printResult('mysql_field_type', mysql_field_type($result, 0));
printResult('mysql_free_result', mysql_free_result($result));
unset($result);
printResult('mysql_get_client_info', mysql_get_client_info());
printResult('mysql_get_host_info', mysql_get_host_info());
printResult('mysql_get_proto_info', mysql_get_proto_info());
printResult('mysql_get_server_info', mysql_get_server_info());
mysql_query('UPDATE `dummy_people` SET `update` = \'' . date('Y-m-d') . '\' WHERE id=\'1\';');
printResult('mysql_info', mysql_info());
mysql_query("INSERT INTO `dummy_people` (`id`, `name`, `address`, `add`, `update`) VALUES (NULL, 'test mustDelete', 'None', '" . date('Y-m-d') . "', '" . date('Y-m-d') . "')");
printResult('mysql_insert_id', mysql_insert_id());
mysql_query('DELETE FROM `dummy_people` WHERE id=\'' . mysql_insert_id() . '\'');
// mysql_list_dbs() use `mysql_query('SHOW DATABASES')` instead.
// mysql_list_fields() use `mysql_query('SHOW COLUMNS FROM `dummy_people`')` instead.
printResult('mysql_list_processes', mysql_list_processes());
// mysql_list_tables() use `mysql_query('SHOW TABLES FROM ' . $db_name)` instead.
$result = mysql_query('SELECT * FROM `dummy_people`');
printResult('mysql_num_fields', mysql_num_fields($result));
printResult('mysql_num_rows', mysql_num_rows($result));
mysql_free_result($result);
unset($result);
printResult('mysql_ping', mysql_ping());
$result = mysql_query('SELECT `name` FROM `dummy_people`');
printResult('mysql_query', $result);
$string = "String with single quote (') and double quote (\") that must be escaped.";
printResult('mysql_real_escape_string', mysql_real_escape_string($string));
unset($string);
printResult('mysql_result', mysql_result($result, 2));
printResult('mysql_set_charset', mysql_set_charset('utf8', $link));
printResult('mysql_stat', mysql_stat());
mysql_free_result($result);
unset($result);
$result = mysql_query('SHOW TABLES FROM `' . $db_name . '`');
printResult('mysql_tablename', mysql_tablename($result, 0));
mysql_free_result($result);
unset($result);
printResult('mysql_thread_id', mysql_thread_id());
$result = mysql_unbuffered_query('SELECT * FROM `dummy_people`');
printResult('mysql_unbuffered_query', $result);
mysql_free_result($result);
unset($result);
$closed = mysql_close($link);
printResult('mysql_close', $closed);
unset($closed, $link);
mysqli-functions.php
Code (PHP)
<?php
require '_config.php';
require '_functions.php';
echo '<h1>Start testing mysqli_xxx() functions</h1>' . PHP_EOL;
$link2 = mysqli_connect('p:'.$db_host, $db_username, $db_password);
printResult('mysqli_connect', $link2);
if ($link2 === false) {
throw new \Exception('Could not connect to the database: ' . mysqli_connect_error());
}
mysqli_close($link2);
unset($link2);
$link = mysqli_connect($db_host, $db_username, $db_password);
printResult('mysqli_connect', $link);
if ($link === false) {
throw new \Exception('Could not connect to the database: ' . mysqli_connect_error());
}
$db_selected = mysqli_select_db($link, $db_name);
printResult('mysqli_select_db', $db_selected);
if ($db_selected === false) {
throw new \Exception('Could not select the database: ' . mysqli_error($link));
}
mysqli_query($link, 'UPDATE `dummy_people` SET `update` = \'' . date('Y-m-d') . '\' WHERE id=\'1\';');
printResult('mysqli_affected_rows', mysqli_affected_rows($link));
printResult('mysqli_character_set_name (mysql_client_encoding)', mysqli_character_set_name($link));
// mysql_create_db() use `mysqli_query($link, 'CREATE ...')` command instead.
$result = mysqli_query($link, 'SELECT * FROM `dummy_people`');
printResult('mysqli_data_seek', mysqli_data_seek($result, 10));
mysqli_free_result($result);
unset($result);
// mysql_db_name() use `mysqli_query($link, 'SELECT DATABASE')` instead.
// mysql_db_query() use `mysqli_query($link, )` instead.
// mysql_drop_db() use `mysqli_query($link, 'DROP DATABASE')` instead.
$result = mysqli_query($link, 'SELECT * FROM `not_exists_table`');
printResult('mysqli_errno', mysqli_errno($link));
printResult('mysqli_error', mysqli_error($link));
unset($result);
// mysql_escape_string() use `mysql_real_escape_string()` instead.
$result = mysqli_query($link, 'SELECT * FROM `dummy_people`');
printResult('mysqli_fetch_array', mysqli_fetch_array($result, MYSQL_NUM));
printResult('mysqli_fetch_assoc', mysqli_fetch_assoc($result));
printResult('mysqli_fetch_field', mysqli_fetch_field($result));
printResult('mysqli_fetch_lengths', mysqli_fetch_lengths($result));
printResult('mysqli_fetch_object', mysqli_fetch_object($result));
printResult('mysqli_fetch_row', mysqli_fetch_row($result));
printResult('mysqli_fetch_field_direct (mysql_field_flags)', mysqli_fetch_field_direct($result, 0)->flags);
printResult('mysqli_fetch_field_direct (mysql_field_len)', mysqli_fetch_field_direct($result, 0)->length);
printResult('mysqli_fetch_field_direct (mysql_field_name)', mysqli_fetch_field_direct($result, 0)->name);
printResult('mysqli_field_seek', mysqli_field_seek($result, 0));
printResult('mysqli_fetch_field_direct (mysql_field_table)', mysqli_fetch_field_direct($result, 0)->table);
printResult('mysqli_fetch_field_direct (mysql_field_type)', mysqli_fetch_field_direct($result, 0)->type);
printResult('mysqli_free_result', mysqli_free_result($result));// NULL
unset($result);
printResult('mysqli_get_client_info', mysqli_get_client_info($link));
printResult('mysqli_get_host_info', mysqli_get_host_info($link));
printResult('mysqli_get_proto_info', mysqli_get_proto_info($link));
printResult('mysqli_get_server_info', mysqli_get_server_info($link));
mysqli_query($link, 'UPDATE `dummy_people` SET `update` = \'' . date('Y-m-d') . '\' WHERE id=\'1\';');
printResult('mysqli_info', mysqli_info($link));
mysqli_query($link, "INSERT INTO `dummy_people` (`id`, `name`, `address`, `add`, `update`) VALUES (NULL, 'test mustDelete', 'None', '" . date('Y-m-d') . "', '" . date('Y-m-d') . "')");
printResult('mysqli_insert_id', mysqli_insert_id($link));
mysqli_query($link, 'DELETE FROM `dummy_people` WHERE id=\'' . mysqli_insert_id($link) . '\'');
// mysql_list_dbs() use `mysqli_query($link, 'SHOW DATABASES')` instead.
// mysql_list_fields() use `mysqli_query($link, 'SHOW COLUMNS FROM `dummy_people`')` instead.
printResult('mysqli_thread_id (mysql_list_processes)', mysqli_thread_id($link));
// mysql_list_tables() use `mysqli_query($link, 'SHOW TABLES FROM ' . $db_name)` instead.
$result = mysqli_query($link, 'SELECT * FROM `dummy_people`');
printResult('mysqli_num_fields', mysqli_num_fields($result));
printResult('mysqli_num_rows', mysqli_num_rows($result));
mysqli_free_result($result);
unset($result);
printResult('mysqli_ping', mysqli_ping($link));
$result = mysqli_query($link, 'SELECT `name` FROM `dummy_people`');
printResult('mysqli_query', $result);
$string = "String with single quote (') and double quote (\") that must be escaped.";
printResult('mysqli_real_escape_string', mysqli_real_escape_string($link, $string));
unset($string);
mysqli_data_seek($result, 2); $row = mysqli_fetch_array($result); printResult('mysqli_data_seek+mysqli_fetch_array (mysql_result)', $row[0]); unset($row);
printResult('mysqli_set_charset', mysqli_set_charset($link, 'utf8'));
printResult('mysqli_stat', mysqli_stat($link));
mysqli_free_result($result);
unset($result);
//$result = mysqli_query($link, 'SHOW TABLES FROM `' . $db_name . '`');
// mysql_tablename() use `mysqli_query($link, 'SHOW TABLES FROM ' . $db_name)` instead.
//mysqli_free_result($result);
//unset($result);
printResult('mysqli_thread_id', mysqli_thread_id($link));
$result = mysqli_query($link, 'SELECT * FROM `dummy_people`', MYSQLI_USE_RESULT);
printResult('mysqli_query with MYSQLI_USE_RESULT constant (mysql_unbuffered_query)', $result);
mysqli_free_result($result);
unset($result);
$closed = mysqli_close($link);
printResult('mysqli_close', $closed);
unset($closed, $link);
|
 |
 |
 |
 |
Date :
2018-10-14 14:23:06 |
By :
mr.v |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เยี่ยมยอดเลยครับ คาราวะ 1 จอก
|
 |
 |
 |
 |
Date :
2018-11-13 12:09:18 |
By :
twc.off |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับ
|
 |
 |
 |
 |
Date :
2018-11-24 16:25:39 |
By :
mutkoko |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
interesting
|
 |
 |
 |
 |
Date :
2019-01-22 17:39:50 |
By :
chirurgientunis |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ยอดเยี่ยมไปเลย ขอบคุณครับ
|
 |
 |
 |
 |
Date :
2019-05-10 01:18:02 |
By :
2254023874716882 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

|
 |
 |
 |
 |
Date :
2019-05-21 13:46:54 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อยากได้ code ที่ค้นหา จาก textbox1 เมื่อมีการคีย์ เปลี่ยนค่า (รหัสนักเรียน)
ให้ทำการค้นหาในฐานข้อมูล (คะแนน) มาแสดงใน textbox2 ในหน้าเดียวกัน
ค้นหานักเรียนสัก 3 คน ( 3 textbox) แสดงคะแนน 3 textbox
แล้วเปรียบเทียบใครเป็นที่ 1 2 3
แล้วนำลำดับไปเขียนลงในฐานข้อมูลต่อ
เป็น mysqli นะครับ
|
 |
 |
 |
 |
Date :
2019-09-12 16:48:56 |
By :
Authorityheart |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

รบกวนชี้แนะด้วยครับ
ค้นหาหลายบทความ เจอแต่ ในหน้าเดียว ค้นหา เรื่องเดียว ตารางเดียว
อันนี้ค้นหา มากกว่า 1 ครั้ง จนปัญญาจริงๆ ครับ
|
 |
 |
 |
 |
Date :
2019-09-16 10:11:17 |
By :
Authorityheart |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ความรู้เลย
ขอบพระคุณมากๆๆๆครับ
|
 |
 |
 |
 |
Date :
2020-04-18 21:37:04 |
By :
12bet |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ความรู้เลย ขอบคุณครับ...
|
 |
 |
 |
 |
Date :
2020-05-17 23:18:53 |
By :
bodog |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เข้ามาอัพเดทตัวอย่างการใช้ mysqli สักหน่อย...
ปกติผมจะใช้ PDO เป็นหลักเพราะมันรองรับ database หลายประเภท และตรงความต้องการมากกว่า แต่เห็นคนไทยใช้ mysqli กันเยอะ ก็เลยเขียนๆตัวอย่างไว้เพื่อใช้อ้างอิงได้ในอนาคต.
Code (PHP)
<?php
// แก้ข้อมูลเหล่านี้เอาเอง ให้ตรงกับของตัวเอง
$dbHost = 'localhost';
$dbUser = 'user';
$dbPass = 'pass';
$dbName = 'test-database-name';
config.php
Code (PHP)
<?php
require 'config.php';
$link = mysqli_connect('p:'.$dbHost, $dbUser, $dbPass);
if (false === $link) {
// ถ้าเชื่อมต่อ DB ไม่ได้ ให้แสดง error ออกมา.
throw new \Exception('Could not connect to the database: ' . mysqli_connect_error());
}
$setCharset = mysqli_set_charset($link, 'utf8');
if (false === $setCharset) {
// ถ้ากำหนด charset ไม่ได้ ให้แสดง error ออกมา.
throw new \Exception('Could not set the character set: ' . mysqli_error($link));
}
unset($setCharset);
$dbSelected = mysqli_select_db($link, $dbName);
if (false === $dbSelected) {
// ถ้าเลือก DB ไม่ได้ ให้แสดง error ออกมา.
throw new \Exception('Could not select the database: ' . mysqli_error($link));
}
unset($dbSelected);
$title = 'ตัวอย่างการแสดงรายการ โดยใช้ mysqli (procedural)';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title><?=$title;?></title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1><?=$title;?></h1>
<table>
<thead>
<tr>
<th>id</th>
<th>ชื่อ</th>
<th>ที่อยู่</th>
<th>วันที่</th>
</tr>
</thead>
<tbody>
<?php
$sql = 'SELECT * FROM `people` ORDER BY `id` DESC LIMIT 0, 20';
// ถ้าใช้ mysqli_query() ควรป้องกัน injection ด้วย mysqli_real_escape_string()
// หรือย้ายไปใช้ mysqli_prepare() แทน mysqli_query().
$result = mysqli_query($link, $sql);
unset($sql);// $sql ไม่ใช้แล้ว unset ทิ้งไป เพื่อคืนพื้นที่ให้หน่วยความจำ
if (false === $result) {
// ถ้า query ไม่ได้ ให้แสดง error ออกมา.
throw new \Exception('Could not run the query: ' . mysqli_error($link));
}
$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
if (is_array($rows)) {
foreach ($rows as $row) {
echo '<tr>' . PHP_EOL;
echo '<td>' . $row['id'] . '</td>' . PHP_EOL;
echo '<td>' . $row['name'] . '</td>' . PHP_EOL;
echo '<td>' . $row['address'] . '</td>' . PHP_EOL;
echo '<td>' . date('Y-m-d H:i:s', (int) $row['date']) . '</td>' . PHP_EOL;
echo '</tr>' . PHP_EOL;
}// endforeach;
unset($row);
}
unset($rows);
// ควรใช้ mysqli_free_result() เพื่อคืนพื้นที่ให้หน่วยความจำ
mysqli_free_result($result);
unset($result);
?>
</tbody>
</table>
</body>
</html>
<?php
$dbClosed = mysqli_close($link);
if (false === $dbClosed) {
// ถ้าสั่งปิดการเชื่อมต่อ DB ไม่ได้ ให้แสดง error ออกมาหรือตรงนี้ไม่ต้องมีก็ได้ (เอาไว้ debug).
throw new \Exception('Could not close the database: ' . mysqli_error($link));
}
unset($dbClosed, $link);
example-procedural-list.php
Code (PHP)
<?php
require 'config.php';
$mysqli = new mysqli($dbHost, $dbUser, $dbPass, $dbName);
if ($mysqli->connect_error) {
// ถ้าเชื่อมต่อ DB ไม่ได้ ให้แสดง error ออกมา.
throw new \Exception('Could not connect to the database: ' . $mysqli->connect_error);
}
$dbSelected = $mysqli->select_db($dbName);
if (false === $dbSelected) {
// ถ้าเลือก DB ไม่ได้ ให้แสดง error ออกมา.
throw new \Exception('Could not select the database: ' . $mysqli->error);
}
unset($dbSelected);
$title = 'ตัวอย่างการแสดงรายการ โดยใช้ mysqli (object oriented)';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title><?=$title;?></title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1><?=$title;?></h1>
<table>
<thead>
<tr>
<th>id</th>
<th>ชื่อ</th>
<th>ที่อยู่</th>
<th>วันที่</th>
</tr>
</thead>
<tbody>
<?php
$sql = 'SELECT * FROM `people` ORDER BY `id` DESC LIMIT 0, 20';
// ถ้าใช้ $mysqli->query() ควรป้องกัน injection ด้วย $mysqli->real_escape_string()
// หรือย้ายไปใช้ $mysqli->prepare() แทน $mysqli->query().
$result = $mysqli->query($sql);
unset($sql);// $sql ไม่ใช้แล้ว unset ทิ้งไป เพื่อคืนพื้นที่ให้หน่วยความจำ
if (false === $result) {
// ถ้า query ไม่ได้ ให้แสดง error ออกมา.
throw new \Exception('Could not run the query: ' . $mysqli->error);
}
$rows = $result->fetch_all(MYSQLI_ASSOC);
if (is_array($rows)) {
foreach ($rows as $row) {
echo '<tr>' . PHP_EOL;
echo '<td>' . $row['id'] . '</td>' . PHP_EOL;
echo '<td>' . $row['name'] . '</td>' . PHP_EOL;
echo '<td>' . $row['address'] . '</td>' . PHP_EOL;
echo '<td>' . date('Y-m-d H:i:s', (int) $row['date']) . '</td>' . PHP_EOL;
echo '</tr>' . PHP_EOL;
}// endforeach;
unset($row);
}
unset($rows);
// ควรใช้ $result->free() เพื่อคืนพื้นที่ให้หน่วยความจำ
$result->free();
unset($result);
?>
</tbody>
</table>
</body>
</html>
<?php
$dbClosed = $mysqli->close();
if (false === $dbClosed) {
// ถ้าสั่งปิดการเชื่อมต่อ DB ไม่ได้ ให้แสดง error ออกมาหรือตรงนี้ไม่ต้องมีก็ได้ (เอาไว้ debug).
throw new \Exception('Could not close the database: ' . $mysqli->error);
}
unset($dbClosed, $mysqli);
example-object-oriented-list.php
ในตัวอย่างมี 2 รูปแบบ อย่างแรกคือแบบ procedural หรือแบบฟังก์ชั่น ซึ่งส่วนใหญ่ที่มาตั้งคำถามมักจะใช้แบบนี้กันมาก
ส่วนอีกแบบคือ object oriented ซึ่งแนะนำให้ใช้แบบนี้จะดีกว่า เพราะไม่ต้องสับสนกับตัวแปร $link ที่อาจจะพลาดไม่ได้เรียกใช้
ทั้ง 2 ไฟล์ เป็นการแสดงรายการจำนวนมากๆ เพื่อเป็นตัวอย่างตั้งแต่การเริ่ม connect, กำหนด character set, เลือกฐานข้อมูล, สั่ง query, ไปจนปิดการเชื่อมต่อ
ทุกขั้นตอนจะมีการตรวจสอบก่อนเสมอว่าคำสั่งทำงานได้จริง ไม่ใช่สั่งแล้วเอามาใช้เลย เป็นเหตุให้เกิด error Quote:expects parameter 1 to be mysqli_result, bool given
ที่มักพบเจอบ่อยๆ. กรณีนี้จะหมดปัญหาไป คือถ้ามีอะไรผิดพลาด มันจะ throw error ออกมาให้รู้ก่อนเลย เราก็แก้ไขไปตามลำดับ.
หวังว่าอัพเดทนี้จะเป็นประโยชน์บ้างสักหน่อยก็ยังดีนะครับ 
|
ประวัติการแก้ไข 2021-04-04 17:12:21
 |
 |
 |
 |
Date :
2021-04-04 14:47:17 |
By :
mr.v |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
created with a different version of MySQL and cannot be read
แก้ไข ได้ไม่ หรือ ต้อง ลง version ใหม่
|
 |
 |
 |
 |
Date :
2023-07-27 07:40:30 |
By :
อำ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 03
|