PDO ឬ PHP Data Object គឺphp library មួយប្រើប្រាស់សំរាប់បង្កើត connection, insert, delete និង update ទិន្នន័យដែលស្រដៀងគ្នាទៅនឹង mysql និង mysqli ហើយវាអាចប្រើប្រាស់បានជាមួយdatabaseបានជាច្រើនលើសពី mysql និងmysqli។ PDO អាចប្រើប្រាស់បានជាមួយdatabase ១២ប្រភេទ ក្រៅពីនេះវាផ្តល់នូវ PreparedStatement ដែលផ្តល់នូវសុវត្ថិភាពក្នុងការទាញយកទិន្នន័យ លុប ឬ បន្ថែមជាដើម។ សូមសិក្សានូវកូដគំរូខាងក្រោមនេះ៖ សំរាប់ភ្ជាប់connection
 try {  
  # MS SQL Server and Sybase with PDO_DBLIB  
  $DBH = new PDO("mssql:host=$host;dbname=$dbname, $user, $pass");  
  $DBH = new PDO("sybase:host=$host;dbname=$dbname, $user, $pass");  
  # MySQL with PDO_MYSQL  
  $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);  
  # SQLite Database  
  $DBH = new PDO("sqlite:my/database/path/database.db");  
 }  
 catch(PDOException $e) {  
   echo $e->getMessage();  
 }  
បិទconnection
 # close the connection  
 $DBH = null;  
សំរាប់បន្ថែមឬកែប្រែទិន្នន័យ
 # STH means "Statement Handle"  
 $STH = $DBH->prepare("INSERT INTO folks ( first_name ) values ( 'Cathy' )");  
 $STH->execute();  
 # no placeholders - ripe for SQL Injection!  
 $STH = $DBH->("INSERT INTO folks (name, addr, city) values ($name, $addr, $city)");  
 # unnamed placeholders  
 $STH = $DBH->("INSERT INTO folks (name, addr, city) values (?, ?, ?);  
 # named placeholders  
 $STH = $DBH->("INSERT INTO folks (name, addr, city) value (:name, :addr, :city)");  
 # assign variables to each place holder, indexed 1-3  
 $STH->bindParam(1, $name);  
 $STH->bindParam(2, $addr);  
 $STH->bindParam(3, $city);  
 # insert one row  
 $name = "Daniel"  
 $addr = "1 Wicked Way";  
 $city = "Arlington Heights";  
 $STH->execute();  
 # insert another row with different values  
 $name = "Steve"  
 $addr = "5 Circle Drive";  
 $city = "Schaumburg";  
 $STH->execute();  
 # the data we want to insert  
 $data = array('Cathy', '9 Dark and Twisty Road', 'Cardiff');  
 $STH = $DBH->("INSERT INTO folks (name, addr, city) values (?, ?, ?);  
 $STH->execute($data);  

►►សូមអរគុណរាល់ការចូលរួមCommentរបស់អ្នក!

 
Top
Don't You Think this Awesome Post should be shared ??
| ស្វែងយល់អំពីPDO សំរាប់បង្កើតជា Web Application [PHP Data Object] |