2015-03-19createphpalternativeformyslq_resultinmysqli(PHP)–PDOTutorialforMysqlDevelopers
內容:
If you are migrating from PHP 5.5 to a newer version of PHP – you might be interested in a MySQL to MySQLi/PDO migration guide – and use the function mysql_result() you might get a notice (in case your error_reporting is set to show deprecated warnings) that this function is deprecate. Since there is no 1:1 alternative you can build your own alternative in MySQLi like shown below:
程式碼:
1
2
3
4
5
6
7
|
function mysqli_result( $result , $row , $field =0){
//adjust the result pointer to that specific row
$result ->date_seek( $row );
// Fetch rsult array
$data = $result ->fetch_array();
return $data [ $field ];
}
|
—–
PDO Tutorial for Mysql Developers
來源:http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers
why user pdo?
mysql_* functions are getting old. For a long time now mysql_* has been at odds with other common SQL database programing interface. It doesn`t support modern SQL database concepts such as prepared statements,stored procs,transactions etc…
connectiong to mysql?
old way:
<?php
$link = mysql_connect(`localhost`,`user`,`pass`);
mysql_select_db(`testdb`,$link);
mysql_set_charset(`UTF-8`,$link);
new way :all you gotta do is create a new PDO object.
PDO`s constructor takes at most 4 parameters–DSN,username,password, and an array of driver options.
A DSN is basically a string of options that tell PDO which driver to use,and the connection details…
<?php
$db = new PDO(`mysql:host=localhost;dbname=testdb;charset=utf8`,`username`,`password`);
NOTE:
If you get an error about character sets, make sure you add the chaset parameter to the DSN. Adding the charset to the DSN is very important for security reasons,most examples you`ll see around leave it out.
MAKE SURE TO INCLUDE THE CHARSET
You can also pass in several driver options as an array to the fourth parameters.
1
2
|
<?php $db = new PDO( `mysql:host=localhost;dbname=testdb;charset=utf8` , `username` , `password` , array (PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
|
you can also set some attributes after PDO construction with the setAttribute method:
<?php $db = new PDO(`mysql:host=localhost;dbname=testdb;charset=utf8`, `username`, `password`); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false
Error Handling
Consider your typical mysql_error handling
1
2
3
|
<?php
//connected to mysql
$result = mysql_query( "select * from table" , $link ) or die (mysql_error( $link ));
|
OR die is a pretty bad way to handle errors, yet this is typical mysql code.You can`t handle die(); as it will just end the scipt abruptly and then echo the error to the screen which you usually do NOT want to show to your end users allowing nasty hackers discover your schema.
PDO has three error handling modes.
本文轉自孤舟夜航之家部落格51CTO部落格,原文連結http://blog.51cto.com/cysky/1622129如需轉載請自行聯絡原作者
cysky
相關文章
- PHP - 什麼是 PHP? 為什麼用 PHP? 有誰在用 PHP?PHP
- [php]php總結(1)PHP
- [php]php總結(2)PHP
- 【PHP】Php連結mysqlPHPMySql
- 指定php版本使用 php命令PHP
- PHP專題-PHP陣列PHP陣列
- 【PHP】面試題整理PHP部分PHP面試題
- 【PHP】PHP7.0.2釋出PHP
- 【PHP】PHP專案製作PHP
- php ?? ?:PHP
- phpPHP
- PHP特性整合(PHP5.X到PHP7.1.x)PHP
- PHP NG (PHP 5.7) 效能比 PHP 5.6 提升近 1 倍PHP
- vulhub-php/php_inclusion_getshellPHP
- PHP-安裝-PHP7.4PHP
- 2.2.php://stdin&php://stdoutPHP
- PHP系列(五)PHP字串處理PHP字串
- PHP系列(十三)PHP會話控制PHP會話
- PHP基礎教程-01 PHP概述PHP
- PHP8:PHP的運算子PHP
- [php]php時間格式化PHP
- PHP版本升級:從php7.1升級到php7.2PHP
- PHP 5.5 / PHP5.6 / PHP-NG 和 HHVM 哪個效能更好?PHP
- PHP 效能優化 - php.ini 配置PHP優化
- 淺談PHP fastcgi和php-fpmPHPAST
- 自學PHP筆記(一)PHP語法PHP筆記
- 自學PHP筆記(五) PHP運算子PHP筆記
- PHP:常用PHP陣列操作函式PHP陣列函式
- 《簡明 PHP 教程》01 關於 PHPPHP
- PHP中php.ini配置詳解PHP
- [php]php錯誤處理機制PHP
- PHP實踐之路(七)PHP函式PHP函式
- PHP 系列:PHP Web 開發基礎PHPWeb
- PHP ExcelPHPExcel
- php反射PHP反射
- PHP OpcachePHPopcache
- php swoolePHP
- [PHP] 黑客PHP黑客