Important Announcement
PubHTML5 Scheduled Server Maintenance on (GMT) Sunday, June 26th, 2:00 am - 8:00 am.
PubHTML5 site will be inoperative during the times indicated!

Home Explore phpreff_final

phpreff_final

Published by ton love, 2021-09-18 06:57:33

Description: คู่มือการใช้งาน PHP

Keywords: phpreff_final,PHP,คู่มือการใช้งาน PHP

Search

Read the Text Version

137 File mysql_error ดา้ นในไฟล์

138 mysqli::get_charset ดึงขอ้ มูลรปู แบบตัวอกั ษรทีใ่ ช้ ดงึ ขอ้ มูลรปู แบบตวั อักษรทีใ่ ช้ ตัวอยา่ ง <?php $db = mysqli_init(); $db->real_connect(“localhost”,”root”,”1111”,”mydb”); var_dump($db->get_charset()); ?> ผลลพั ท์ mysqli::get_client_info ดึงขอ้ มลู เวอรช์ ันของ mysql client ในแบบของตวั อักษร ดงึ ขอ้ มูลเวอร์ชันของ mysql client

139 ตัวอยา่ ง <?php printf(“Client library version: %s\\n”, mysqli_get_client_inf o()); ?> ผลลพั ท์ mysqli::get_connection_stats ดึงขอ้ มูลประวัติการใช้งาน ฐานขอ้ มลู ดงึ ขอ้ มูลประวัติการใช้งานฐานขอ้ มูล ตัวอยา่ ง <?php $link = mysqli_connect(); print_r(mysqli_get_connection_stats($link)); ?> ผลลพั ท์

140 mysqli::kill หยุดการทางานของ mysql หยุดการทางานของ mysql ตวั อยา่ ง <?php $mysqli = new mysqli(“localhost”, “root”, “1111”, “mydb”); $thread_id = $mysqli->thread_id; $mysqli->kill($thread_id); if (!$mysqli->query(“CREATE TABLE mydistrinct2 LIKE mydistri nct”)) { printf(“Error: %s\\n”, $mysqli->error); exit; } $mysqli->close(); ?> ผลลพั ท์

141 mysqli::ping ทดสอบสถานะการทางานของ mysql ทดสอบสถานะการทางานของ mysql ตัวอยา่ ง <?php $mysqli = new mysqli(“localhost”, “root”, “1111”, “mydb”); if ($mysqli->ping()) { printf (“Our connection is ok!\\n”); } else { printf (“Error: %s\\n”, $mysqli->error); } $mysqli->close(); ?> ผลลพั ท์

142 mysqli::prepare จัดเตรียมคาสัง่ ทีจ่ ะใช้ควิ รี่ prepare คอื ฟั งกช์ นั ทใี่ ช้ จัดเตรียมคาสงั่ ทีจ่ ะใช้ควิ รี่ ตวั อยา่ ง <?php $mysqli = new mysqli(“localhost”, “root”, “1111”, “mydb”); $District_Name = “เสาชงิ ช้า”; if ($stmt = $mysqli->prepare(“SELECT District_Code FROM district WHERE District_Name=?”)) { $stmt->bind_param(“s”, $District_Name); $stmt->execute(); $stmt->bind_result($District_Code); $stmt->fetch(); printf(“%s is in district %s\\n”, $District_Name, $District_Code); $stmt->close(); } $mysqli->close(); ?> ผลลพั ท์

143 mysqli::query ทาการควิ รีข่ อ้ มูล ทาการคิวรีข่ อ้ มลู จาก Database ตัวอยา่ ง <?php $mysqli = new mysqli(“localhost”, “root”, “1111”, “mydb”); if ( $result = $mysqli->query(“select District_name from dis trict”) ) { printf(“Number of Record Table District : “.$result->num _rows); } $mysqli->close(); ?> ผลลพั ท์

144 mysqli::real_connect เปิดการเช่อื มตอ่ กบั mysql เปิดการเช่อื มตอ่ กบั mysql ตวั อยา่ ง <?php $mysqli = mysqli_init(); if (!$mysqli->real_connect(‘localhost’, ‘root’, ‘1111’, ‘myd b’)) { die(‘Connect Error (‘ . mysqli_connect_errno() . ‘) ‘. mysql i_connect_error()); } echo ‘Success… ‘ . $mysqli->host_info . “\\n”; $mysqli->close(); ?> ผลลพั ท์

145 mysqli::real_escape_string ลบอกั ขระตอ้ งห้ามจากเง่ือนไขทจี่ ะ ใช้คิวรี่ ลบอกั ขระตอ้ งห้ามจากเง่อื นไขทจี่ ะใช้คิวรี่ ตวั อยา่ ง <?php $mysqli = mysqli_init(); $mysqli = new mysqli(“localhost”, “test1”, “1111”, “mydata”) ; $District_Name = “ I’m testing \\n \\r ‘’ “; echo $District_Name ; //ขอ้ มูลทยี่ ังไมไ่ ดใ้ ช้ real_escape_string echo “<br>”; if (!$mysqli->query(“INSERT into District (District_Name) VA LUES (‘$District_Name’)”)) { // Insert ขอ้ มูลทยี่ ังไมไ่ ดใ้ ช้ real_escape _string printf(“Insert Error: %s\\n”, $mysqli->sqlstate); // Error เน่อื งจากมขี อ้ มลู ทีไ่ มส่ ามารถ insert ได้ เช่น ‘ , “ , \\n , \\r } echo “<br>”; $District_Name = $mysqli->real_escape_string($District_Name) ;

146 echo $District_Name ; //ขอ้ มูลทใี่ ช้ real_escape_string echo “<br>”; if ($mysqli->query(“INSERT into District (District_Name) VAL UES (‘$District_Name’)”)) { // Insert ขอ้ มลู ทใี่ ช้ real_escape_strin g printf(“%d Row inserted.\\n”, $mysqli->affected_rows); } $mysqli->close(); ?> ผลลพั ท์ ขอ้ มูลทถี่ กู Insert ลง mysql

147 mysqli::real_query ทาการคิวรีข่ อ้ มลู ทาการคิวรีข่ อ้ มลู ตวั อยา่ ง <?php $mysqli = new mysqli(“localhost”, “test1”, “1111”, “mydata”) ; echo “Test Function query <br>”; $result = $mysqli->query(“SELECT tb_id , tb_name FROM mytab le”) ; //echo “Test Function result “.$result -> num_rows; // ตอ้ งมี f unction มาใช้เพ่อื เเสดงคา่ while ($row = $result->fetch_row()) { printf (“%s (%s)\\n”, $row[0], $row[1]); echo “<br>” ; } mysqli_free_result($result); echo “<br>” ; echo “Test Function real_query <br>”; $result = $mysqli->real_query(“SELECT tb_id , tb_name FROM m ytable”) ; echo “Test Function return “.$result ; // เเสดงคา่ True(1) หรือ Fal

148 se(0) echo “<br>” ; $result = $mysqli->store_result(); while ($row = $result->fetch_row()) { printf (“%s (%s)\\n”, $row[0], $row[1]); echo “<br>” ; } mysqli_free_result($result); ?> ผลลพั ท์ mysqli::rollback ยอ้ นการทางานของทรานแซคชนั่ ยอ้ นการทางานของทรานแซคชนั่ ตวั อยา่ ง <?php $mysqli = new mysqli(“localhost”, “root”, “1111”, “mydb”); $mysqli->autocommit(false); $mysqli->query(“INSERT INTO mydistrict SELECT FROM district LIMIT 50” );

149 $mysqli->commit(); $mysqli->query(“DELETE FROM mydistrict”); if ($result = $mysqli->query(“SELECT COUNT() FROM mydistrict”)) { $row = $result->fetch_row(); printf(“%d rows in table mydistrict.\\n”, $row[0]); $result->close(); } $mysqli->rollback(); if ($result = $mysqli->query(“SELECT COUNT(*) FROM mydistrict”)) { $row = $result->fetch_row(); printf(“%d rows in table mydistrict (after rollback).\\n”, $row[0]) ; $result->close(); } $mysqli->close(); ?> ผลลพั ท์ mysqli::select_db เลือกฐานขอ้ มลู ทจี่ ะใช้ เลอื กฐานขอ้ มูลทจี่ ะใช้ ตวั อยา่ ง <?php $mysqli = new mysqli(“localhost”, “root”, “1111”, “mydb”); if ($result = $mysqli->query(“SELECT DATABASE()”)) { $row = $result->fetch_row(); printf(“Default database is %s.\\n”, $row[0]);

150 $result->close(); } echo “<br>”; $mysqli->select_db(“mysql”); if ($result = $mysqli->query(“SELECT DATABASE()”)) { $row = $result->fetch_row(); printf(“Default database is %s.\\n”, $row[0]); $result->close(); } $mysqli->close(); ?> ผลลพั ท์ mysqli::set_charset ตัง้ คา่ รูปแบบตวั อกั ษร ตงั้ คา่ รปู แบบตวั อักษร ตัวอยา่ ง <?php $mysqli = new mysqli(“localhost”, “root”, “1111”, “mydb”); printf(“Initial character set: %s\\n”, $mysqli->character_set _name());

151 echo “<br>”; if (!$mysqli->set_charset(“utf8”)) { printf(“Error loading character set utf8: %s\\n”, $mysqli ->error); exit(); } else { printf(“Current character set: %s\\n”, $mysqli->character _set_name()); } $mysqli->close(); ?> ผลลพั ท์ stat ดึงขอ้ มูลแบบสถติ ิต่างๆ ของไฟล์ ตัวอยา่ ง <?php var_dump(stat(‘current.php’));

152 ?> ผลลพั ท์

153

154 php base64_decode ถอดรหัส base64 ตวั อยา่ ง <?php $str = ‘VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==’; echo base64_decode($str); ?> ผลลพั ท์ base64_encode เขา้ รหัสดว้ ย base64 ตวั อยา่ ง <?php $str = ‘This is an encoded string’; echo base64_encode($str); ?> ผลลพั ท์

155 dns_get_record ดงึ ขอ้ มูลของโดเมน คอื ฟั งกช์ นั ที่ ใช้สาหรบั ดงึ ขอ้ มลู ตา่ งๆ ของโดเมน ตวั อยา่ ง <?php var_dump(dns_get_record(“php.net”)); ?> ผลลพั ท์ gethostbyname ดึง ip ของโดเมน gethostbyname คือ ฟั งกช์ นั ทใ่ี ช้ดึง ip โดยใช้ช่อื ของเวบ็ หรอื ที่เรยี กโดเมนเป็นพารามเิ ตอร์

156 ตวั อยา่ ง <?php var_dump(gethostbyname(‘www.facebook.com’)); ?> ผลลพั ท์ getprotobyname-ดึงคา่ port ทีโ่ ปรโตคอลนัน้ ใช้ ตวั อยา่ ง <?php header(‘Content-Type: text/html; charset=utf-8’); $protocolNumber=getprotobyname(‘udp’) ; echo “หมายเลขของ protocol udp คอื “. $protocolNumber ; ?> ผลลพั ท์ get_headers ดึงขอ้ มลู ของคาร้องขอ ตวั อยา่ ง

157 <?php $url = ‘http://www.facebook.com’; var_dump(get_headers($url)); ?> ผลลพั ท์ get_meta_tags-ดงึ ขอ้ มูลในสว่ นของแทก๊ meta ของหนา้ เวบ็ คอื ดึงขอ้ มลู ในสว่ นของแทก๊ meta ของหนา้ เวบ็ ซ่งึ จะอยใู่ นแทก๊ head ของหนา้ เวบ็ นัน้ ตวั อยา่ ง <?php var_dump(get_meta_tags(‘http://www.adobe.com/‘)); ?> ผลลพั ท์

158 header สร้างขอ้ มูลในสว่ นของ http header ใช้สรา้ งขอ้ มูลกากบั ขอ้ มลู ทีจ่ ะส่งกลบั ไปให้ผใู้ ช้งาน หรอื response โดยวิธีการใช้งานหลากหลาย ตวั อยา่ งที่ 1 การสง่ สถานะ 500 <?php header(“HTTP/1.0 500 Not Found”); ?> ผลลพั ท์

159 headers_sent ตรวจสอบวา่ ไดส้ ง่ คา่ header ไปหรอื ยัง ตวั อยา่ ง <?php if(headers_sent()) { // ถ้าสง่ ไปแลว้ echo “Please go to yahoo.com”; } else{ //ถา้ ยังไมไ่ ด้ส่ง header ให้ไปท่ี header(‘Location: http://yahoo.com‘); exit; } ?> ผลลพั ท์

160 http_build_query สร้างควิ รี่สตริง ตวั อยา่ ง <?php $data = array(‘foo’=>’php’, ‘baz’=>’is’, ‘cow’=>’languge’, ‘php’=>’hypertext processor’); echo http_build_query($data) ; ?> ผลลพั ท์ http_response_code ดึงหรือตงั้ คา่ คาตอบกลบั จากเซริ ์ฟเวอร์ ตวั อยา่ ง <?php var_dump(http_response_code()); ?>

161 ผลลพั ท์ จะเหน็ วา่ คา่ ทไี่ ดก้ ลบั มาคือ 200 ทแี่ ปลวา่ การทางานสาเรจ็ ob_clean ลบคา่ output buffer ob_clean คอื ฟั งกช์ ันทใี่ ช้ลบขอ้ มูลใน output buffer แตจ่ ะไมป่ ิดการใช้งานดว้ ย เหมอื นกบั ฟั งกช์ นั ob_end_clean ตวั อยา่ ง <?php ob_start(); echo ‘Text that won\\’t get displayed.’; ob_clean(); ?> ผลลพั ท์ ไมม่ ีการแสดงคา่ ออกมา ob_end_clean ลบคา่ output buffer และปิดการใช้งาน ob_end_clean เป็นฟั งกช์ นั ใช้ ลบคา่ output buffer และปิดการใช้งาน ตวั อยา่ ง

162 <?php ob_start(); echo ‘Text that won\\’t get displayed.’; ob_end_clean(); ?> ผลลพั ท์ ไมม่ คี า่ ใดๆ ออกมาเพราะทาการลบขอ้ มลู ใน buffer ออกไปแล้ว parse_url ตัดแบง่ url ออกเป็นอาเรย์ ตวั อยา่ ง <?php $url = ‘https://www.google.co.th/webhp?sourceid=chrome-instant&ion=1&e spv=2&ie=UTF-8‘; var_dump(parse_url($url)); ?> ผลลพั ท์

163 rawurlencode เขา้ รหัส url ตามที่กาหนด เขา้ รหัส url ตามทกี่ าหนด ตวั อยา่ ง <?php echo ‘Before : foo @+%/‘; echo ‘<br>’; echo ‘After : ‘.rawurlencode(‘foo @+%/‘); ?> ผลลพั ท์

164 syslog สร้างประวัตกิ ารทางาน สร้างประวัติการทางาน ตวั อยา่ ง <?php openlog(“myscriptlog”, LOG_PID | LOG_PERROR , LOG_USER ); $access = date(“Y/m/d H s”); var_dump(syslog(LOG_INFO, “Unauthorized client: $access {$_SERVER[‘REM OTE_ADDR’]} ({$_SERVER[‘HTTP_USER_AGENT’]})”) ); closelog(); ?> ผลลพั ท์ เปิด event viewer ข้ึนมา จะเจอลอ็ กทีส่ ัง่ รันไป

165 setcookie สง่ cookie ไปให้ผใู้ ช้งาน ตวั อยา่ งที่ 1 การใช้งานเบ้อื งต้น <?php $value = ‘Chocolate Cookie is crazy delicious’; setcookie(“TestCookie”, $value); ?> ผลลพั ท์

166 urldecode ถอดรหสั ให้กับคิวรีส่ ตรงิ ตวั อยา่ ง <?php $before = urlencode(‘What does “yolo” mean?’); echo ‘url before decode <br>’.$before.’<br><br><br>’; $after = urldecode($before); echo ‘url after decode <br>’.$after; ?> ผลลพั ท์

167

168

169 PDO::__construct การสร้างออปเจคตเ์ พ่อื เตรยี มการควิ รี่ ตัวอยา่ งการสร้างออปเจคตเ์ พ่อื เตรยี มการควิ รี่ ตวั อยา่ ง <?php try { $dbh = new PDO(‘mysql:host=localhost;dbname=mydata’, ‘test1’, ‘111 1’);//PDO::__construct echo “Connection Success” ; } catch (PDOException $e) { echo ‘Connection failed: ‘ . $e->getMessage(); } ?> ผลลพั ท์ PDO::beginTransaction เปิดการใช้งานทรานแซคชัน่ ปิดการใช้งาน autocommit ตวั อยา่ ง <?php $dbh = new PDO(‘mysql:host=localhost;dbname=mydata’, ‘test1’, ‘1111’); $dbh->beginTransaction();

170 $del = $dbh->prepare(“Insert into district ( district_name) values ( ‘Test Name’) “ ); $del->execute(); $del = $dbh->prepare(“select from district”); $del->execute(); $count = $del->rowCount(); echo “row count : “.($count) ;// แสดงจานวน row ก่อน rollback $dbh->rollBack(); echo “ “; $del = $dbh->prepare(“select from district”); $del->execute(); $count = $del->rowCount(); echo “row count : “.($count) ;// แสดงจานวน row หลัง rollback ?> ผลลพั ท์ PDO::commit ใช้สาหรับปลอ่ ยให้ผลการทางานของทรานแซคชนั่ ถกู บันทกึ ใช้สาหรับปล่อยให้ผลการทางานของทรานแซคชัน่ ถูกบันทึก ตวั อยา่ ง <?php $dbh = new PDO(‘mysql:host=localhost;dbname=mydata’, ‘test1’, ‘1111’); $dbh2 = new PDO(‘mysql:host=localhost;dbname=mydata’, ‘test1’, ‘1111’); $dbh->beginTransaction();

171 $sth = $dbh->prepare(“Insert into district ( district_name) values ( ‘aass’) “); $sth->execute(); $sth = $dbh->prepare(‘SELECT from district’); $sth->execute(); $count = $sth->rowcount(); echo (“Obj1 rows count $count rows.\\n”);// จานวน Row ของ Obj1 เม่อื ไม่ได้ commit $sth = $dbh2->prepare(‘SELECT from district’); $sth->execute(); $count = $sth->rowcount(); echo (“Obj2 rows count $count rows.\\n”);// จานวน Row ของ Obj2 เม่อื ไมไ่ ด้ commit echo “<br>” ; $sth = $dbh->prepare(“Insert into district ( district_name) values ( ‘aass’) “); $sth->execute(); $sth = $dbh->prepare(‘SELECT from district’); $sth->execute(); $count = $sth->rowcount(); echo (“Obj1 rows count $count rows.\\n”); // จานวน Row ของ Obj1 เม่อื commit $dbh->commit(); $sth = $dbh2->prepare(‘SELECT from district’); $sth->execute(); $count = $sth->rowcount(); echo (“Obj2 rows count $count rows.\\n”);// จานวน Row ของ Obj2 เม่อื commit ?> ผลลพั ท์

172 PDO::errorCode ดึงรหสั ของขอ้ ผิดพลาด ดึงรหัสของขอ้ ผดิ พลาด ตวั อยา่ ง <?php $dbh = new PDO(‘mysql:host=localhost;dbname=mydata’, ‘test1’, ‘1111’); $numRows = $dbh->exec(“DELETE FROM tesr WHERE ID between 6 and 17”); echo “Error code : “.$dbh->errorCode(); ?> ผลลพั ท์ PDO::errorInfo ดงึ ขอ้ มลู เกยี่ วกบั ขอ้ ผดิ พลาดทัง้ หมด ใช้ดงึ ขอ้ มูลเกี่ยวกบั ขอ้ ผดิ พลาดทงั้ หมด ตวั อยา่ ง <?php $dbh = new PDO(‘mysql:host=localhost;dbname=mydata’, ‘test1’, ‘1111’); $numRows = $dbh->exec(“DELETE FROM tesr WHERE ID between 6 and 17”); var_dump ($dbh->errorInfo()); ?>

173 ผลลพั ท์ PDO::getAvailableDrivers ดึงรายช่อื ฐานขอ้ มูลที่ PDO สนบั สนนุ getAvailableDrivers คือ ฟั งกช์ นั ทใี่ ช้ ดงึ รายช่อื ฐานขอ้ มูลที่ PDO สนบั สนนุ ตวั อยา่ ง <?php $dbh = new PDO(‘mysql:host=localhost;dbname=mydata’, ‘test1’, ‘1111’); var_dump($dbh->getAvailableDrivers()); ?> ผลลพั ท์

174 inTransaction คือ ฟั งกช์ นั ทีใ่ ช้ ตรวจสอบวา่ ตอนนมี้ ีการใช้งานทรานแซคชนั่ อยู่ หรือไม่ คอื ฟั งกช์ นั ทีใ่ ช้ ตรวจสอบวา่ ตอนนีม้ กี ารใช้งานทรานแซคชัน่ อยหู่ รือไม่ ตวั อยา่ ง <?php echo “Autocommit off : “; var_dump($dbh->inTransaction()); echo “<br>”; echo “Set Autocommit off : “; var_dump($dbh->beginTransaction()); echo “<br>”; echo “Autocommit off : “; var_dump($dbh->inTransaction()); ?> ผลลพั ท์ PDO::lastInsertId ดึงคา่ ไอดีของขอ้ มลู ตัวสดุ ทา้ ยทไี่ ด้บนั ทกึ ไปในครัง้ ลา่ สดุ lastInsertId คือ ฟั งกช์ ันทีใ่ ช้ ดึงคา่ ไอดีของขอ้ มลู ตัวสดุ ทา้ ยทีไ่ ดบ้ ันทึกไปในครัง้ ล่าสดุ

175 ตัวอยา่ ง <?php $dbh = new PDO(‘mysql:host=localhost;dbname=mydata’, ‘test1’, ‘1111’); $stmt = $dbh->prepare( “ insert into district ( district_name) values ( ‘Test Test’) “); $stmt->execute(); $lastId = $dbh->lastInsertId(); echo “Insert Id : “.$lastId; ?> ผลลพั ท์ mysqli::prepare จัดเตรียมคาสัง่ ที่จะใช้คิวรี่ prepare คือ ฟั งกช์ นั ทีใ่ ช้ จดั เตรียมคาสงั่ ทีจ่ ะใช้คิวรี่ ตวั อยา่ ง <?php $mysqli = new mysqli(“localhost”, “root”, “1111”, “mydb”); $District_Name = “เสาชิงช้า”; if ($stmt = $mysqli->prepare(“SELECT District_Code FROM district WHERE District_Name= ?”)) { $stmt->bind_param(“s”, $District_Name); $stmt->execute(); $stmt->bind_result($District_Code); $stmt->fetch(); printf(“%s is in district %s\\n”, $District_Name, $District_Code); $stmt->close(); }

176 $mysqli->close(); ?> ผลลพั ท์ PDO::query เรมิ่ การดงึ ขอ้ มลู จากฐานขอ้ มลู query คือ ฟั งกช์ ันทใี่ ช้เริม่ การดงึ ขอ้ มูลจากฐานขอ้ มูล ตวั อยา่ ง <?php $dbh = new PDO(‘mysql:host=localhost;dbname=mydata’, ‘test1’, ‘1111’); $sql = ‘SELECT district_id, district_name FROM district ORDER BY district_id’; foreach ($dbh->query($sql) as $row) { echo $row[‘district_id’] . “\\t”; echo $row[‘district_name’] . “\\t”; echo “<br>” ; } ?> ผลลพั ท์

177 PDO::quote กรองขอ้ มูลที่จะบันทกึ quote คือ ฟั งกช์ นั ทีใ่ ช้ใส่ Quote( ‘ ) เพ่ือใช้ในการควิ รี่ ตวั อยา่ ง <?php $dbh = new PDO(‘mysql:host=localhost;dbname=mydata’, ‘test1’, ‘1111’); $string = ‘Nice’; echo “Unquoted string: $string\\n”; echo “<br>”; echo “Quoted string: “ . $dbh->quote($string) . “\\n”; ?> ผลลพั ท์

178 PDO::quote กรองขอ้ มูลทีจ่ ะบนั ทึก quote คอื ฟั งกช์ นั ทใี่ ช้ใส่ Quote( ‘ ) เพ่ือใช้ในการคิวรี่ ตวั อยา่ ง <?php $dbh = new PDO(‘mysql:host=localhost;dbname=mydata’, ‘test1’, ‘1111’); $string = ‘Nice’; echo “Unquoted string: $string\\n”; echo “<br>”; echo “Quoted string: “ . $dbh->quote($string) . “\\n”; ?> ผลลพั ท์ PDO::rollBack ยกเลกิ การทาทรานแซคชนั่ rollBack คอื ฟั งกช์ นั ทใี่ ช้ ยกเลกิ การทาทรานแซคชัน่ ตวั อยา่ ง <?php $dbh = new PDO(‘mysql:host=localhost;dbname=mydata’, ‘test1’, ‘1111’); $dbh->beginTransaction(); $del = $dbh->prepare(“Insert into district ( district_name) values ( ‘Test Name’) “

179 ); $del->execute(); $del = $dbh->prepare(“select from district”); $del->execute(); $count = $del->rowCount(); echo “row count : “.($count) ;// แสดงจานวน row ก่อน rollback $dbh->rollBack(); echo “ “; $del = $dbh->prepare(“select from district”); $del->execute(); $count = $del->rowCount(); echo “row count : “.($count) ;// แสดงจานวน row หลงั rollback ?> ผลลพั ท์

180 PDOStatement::bindColumn จัดการขอ้ มลู ในคอลัมนโ์ ดยการผูกเขา้ ไป กบั ตัวแปร bindColumn คอื ฟั งกช์ นั ทใี่ ช้ จัดการขอ้ มลู ในคอลัมน์โดยการผกู เขา้ ไปกับตวั แปร ตวั อยา่ ง <?php $dbh = new PDO(‘mysql:host=localhost;dbname=mydata’, ‘test1’, ‘1111’); function readData($dbh) { $sql = ‘SELECT distinct district_id, district_name FROM district’; try { $stmt = $dbh->prepare($sql); $stmt->execute(); $stmt->bindColumn(1, $district_id); $stmt->bindColumn(2, $district_name); while ($row = $stmt->fetch(PDO::FETCH_BOUND)) { $data = “\\t” . $district_id . “\\t” . $district_name . “\\n <br>”; print $data; } } catch (PDOException $e) { print $e->getMessage(); } } readData($dbh); ?> ผลลพั ท์

181 bindParam จัดการขอ้ มลู ในคอลัมนโ์ ดยการผูกเขา้ ไปกับพารามเิ ตอร์ แบบ อ้างอิงหนว่ ยความจาในขณะนนั้ คอื ฟั งกช์ นั ทใี่ ช้จดั การขอ้ มูลในคอลัมนโ์ ดยการผูกเขา้ ไปกับพารามิเตอร์ แบบอา้ งอิงหนว่ ยความจาใน ขณะนนั้ ตวั อยา่ ง <?php $dbh = new PDO('mysql:host=localhost;dbname=mydata', 'test1', '1111'); $district_id = 510; $district_name = 'Asqw1'; $sth = $dbh->prepare('SELECT district_id, district_name FROM District WHERE district_id < :district_id and district_name = :d istrict_name'); $sth->bindParam(':district_id', $district_id, PDO::PARAM_INT); $sth->bindParam(':district_name', $district_name, PDO::PARAM_STR, 12); $sth->execute(); $sth->bindColumn(1, $district_id); $sth->bindColumn(2, $district_name); while ($row = $sth->fetch(PDO::FETCH_BOUND)) { $data = \"\\t\" . $district_id . \"\\t\" . $district_name . \"\\n <br>\"; print $data; } ?> ผลลพั ท์

182 PDOStatement::bindValue จดั การขอ้ มลู ในคอลมั นโ์ ดยการผกู เขา้ ไปกบั คา่ ตายตัว bindValue คือ ฟั งกช์ นั นีใ้ ช้ จดั การขอ้ มลู ในคอลมั น์โดยการผูกเขา้ ไปกับคา่ ตายตวั ตวั อยา่ ง <?php $dbh = new PDO(‘mysql:host=localhost;dbname=mydb’, ‘test_1’, ‘1111’); $sth = $dbh->prepare(‘SELECT id, name FROM friends’); $sth->execute(); echo “All Values<br>”; var_dump( $sth-> fetchall()); //เเสดงค่าใน table friends echo “<br><br>Test PDO::BindValue<br>”; $district_id = 1 ; // เง่อื นไข where id = 1 $sth = $dbh->prepare(‘SELECT id, name FROM friends WHERE id = :d_id’); $sth->bindValue(‘:d_id’, $district_id); $district_id = 2 ; // เง่อื นไข where id = 2 $sth->execute(); var_dump($sth-> fetchall()); // จะไดค้ ่าครงั้ แรกท่ีระบุ ?> ผลลพั ท์

183

184

185 preg_replace ใช้งาน regular expression ในการคน้ หาและแกไ้ ข ตวั อยา่ ง <?php $copy_date = \"Copyright 1999\"; $copy_date = preg_replace(\"([0-9]+)\", \"2000\", $copy_date); print $copy_date; ?> ผลลพั ท์ preg_match_all ใช้งาน regular expression ในการคน้ หา โดย คน้ หาจนเจอทตี่ รงหมดทกุ ตวั ใช้งาน regular expression ในการคน้ หา โดยคน้ หาจนเจอทีต่ รงหมดทุกตัว ตวั อยา่ ง <?php $userinfo = “Name: <b>John Poul</b> <br> Title: <b>PHP Guru</b>”; preg_match_all (“/<b>(.*)<\\/b>/U”, $userinfo, $matches); vat_dump($matches); echo “<br><br> Result array all <br><br>”; echo $matches[0][0].”<br>”; echo $matches[0][1].”<br>”; echo $matches[1][0].”<br>”;

186 echo $matches[1][1].”<br>”; ?> ผลลพั ท์ preg_replace ใช้งาน regular expression ในการคน้ หาและแกไ้ ข ตวั อยา่ ง <?php $copy_date = “Copyright 1999”; $copy_date = preg_replace(“([0-9]+)”, “2000”, $copy_date); print $copy_date; ?> ผลลพั ท์


Like this book? You can publish your book online for free in a few minutes!
Create your own flipbook