來源:http://www.bestwebframeworks.com/tutorials/php/152/create-php-alternative-for-mysql_result-in-mysqli/
內容:
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