日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

2015-03-19 create php alternative for myslq_result in mysqli(PHP)--PDO Tutorial for Mysql Developers

發布時間:2025/3/8 数据库 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 2015-03-19 create php alternative for myslq_result in mysqli(PHP)--PDO Tutorial for Mysql Developers 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

來源: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

總結

以上是生活随笔為你收集整理的2015-03-19 create php alternative for myslq_result in mysqli(PHP)--PDO Tutorial for Mysql Developers的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。