จากบทความที่แล้ว เรื่อง การใช้ adodb บทความนี้ผมเปรียบเทียบการใช้ ADOdb กับ function Mysql
ได้ พูดถึงการติดตั้ง และการใช้งานเบื้องต้น ถึงระดับกลางไปแล้ว แต่หลายท่าน ที่เพิ่งหัดเขียน php คงยังรู้สึกยากๆ อยู่ที่จะเปลี่ยนการใช้function mysql_XXX ทั้งหลายไป เป็น การ ใช้ function ที่ ADOdb จัดเตรียมให้
function |
Mysql_xxx | ADOdb |
การ connect | mysql_connect("localhost", "root", "password"); | include("adodb.inc.php"); $db = NewADOConnection('mysql'); $db->Connect("localhost", "root", "password", "mydb"); |
การ รันคำสั่ง SQL |
mysql_query($sql) |
$db->Execute($sql) |
การวน loop เพื่อแสดงผลข้อมูลทั้งหมด | while ($fields = mysql_fetch_row($result)) { // วนลูป field ในแถวข้อมูล for ($i=0, $max=sizeof($fields); $i < $max; $i++) { print $fields[$i].' '; } print " n"; } |
while (!$result->EOF) { for ($i=0, $max=$result->FieldCount(); $i < $max; $i++) print $result->fields[$i].' '; $result->MoveNext(); print " n"; } |
การตรวจจับ error ของคำสั่ง sql | $result = mysql_query($sql); if ($result === false) die("failed"); |
$result = $db->Execute($sql); if ($result === false) die("failed"); |
การยัดค่าใส่ตัวแปล array |
while ( $rs = mysql_fetch_row($result) ) { # process $arr } |
$rs = $db->Execute($sql); if ($rs) while ($arr = $rs->FetchRow()) { # process $arr } |
การรับค่าทั้งหมด ของผลการรันคำสั่ง sql |
- |
$db->GetArray(), $db->GetRow($sql) $db->GetAll($sql) |
สร้าง list menu จากผลของการรันคำสั่ง sql | - |
$rs = $conn->Execute($sql); print $rs->GetMenu('ชื่อlistmanu','ค่าที่ถูกselect',true); data ต้อง เป็น ( name,id ) |
การแสดงผล ข้อมูล ที่ละหน้า | ต้องเขียน โค้ดแบ่งหน้าเอง |
include_once('adodb.inc.php'); include_once('adodb-pager.inc.php'); session_start(); $db = NewADOConnection('mysql'); $db->Connect('localhost','root','','xphplens'); $sql = "select * from adoxyz "; $pager = new ADODB_Pager($db,$sql); $pager->Render($rows_per_page=5); |
นับจำนวน recode ทั้งหมด | mysql_num_rows($result) |
$db->RecordCount($result) |
แสดงผล error ด้วย pear | ต้องเขียน ดึง pear มาให้เอง |
include('adodb-errorpear.inc.php'); if error แสดง ค่าด้านล้างๆด้เลย $e = ADODB_Pear_Error(); echo ' ',$e->message,' '; |
การ รัน sql เพื่อ จำกัด ประริมาณข้อมูล | ต้องเขียนที่ คำสั่ง sql |
$db->SelectLimit($sql,$numrows=-1,$offset=-1,$inputarr=false) |
ขอจบแค่นี้ก่อน นะ |