来源: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",$linkor 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相关推荐

  1. agentzh 的 Nginx 教程(版本 2015.03.19) 第一篇

    转载:http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html#02-NginxDirectiveExecOrder10 目录 缘 ...

  2. Contest2071 - 湖南多校对抗赛(2015.03.28)

    Contest2071 - 湖南多校对抗赛(2015.03.28) 本次比赛试题由湖南大学ACM校队原创 http://acm.csu.edu.cn/OnlineJudge/contest.php?c ...

  3. 黄宇清 java_二面笔记 2015.9.19

    二面笔记 2015.9.19 总结 1.杜佰超(x) 不太清楚目标,按兴趣来,态度比较诚恳. 怕困难? 没办法快速想到一件事情自己一直坚持的 有说大话的嫌疑(我不敢说自己能付出别人百倍的努力...) ...

  4. 湖南多校对抗赛(2015.03.28)

    Contest2071 - 湖南多校对抗赛(2015.03.28) Contest2071 - 湖南多校对抗赛(2015.03.28) 本次比赛试题由湖南大学ACM校队原创 http://acm.cs ...

  5. 2020.03.19基于ES6.8.5,IK6.8.5,Kibana6.8.5的测试命令

    2020.03.19基于ES6.8.5,IK6.8.5,Kibana6.8.5的测试命令#全部查询 GET _search {"query": {"match_all&q ...

  6. 湖南多校对抗赛(2015.03.28) H SG Value

    题意:给你一个集合,动态插入 ,动态询问,然后问你这个集合的sg值(这个集合用加法运算不能产生的那个最小正整数)是多少. 解题思路:假设我们现在的这个SG值是 x 1)现在插入集合里面一个数v   如 ...

  7. 去小机化思维(二)--【软件和信息服务】2015.03

    (接续上篇-10043-去小机化思维(一)) 上次分析了去小机化迁移的前两个方面:1.迁移时机的选择和优势考量:2.硬件系统的迁移评估.下面接下来分析另外的四个方面,包括:3.基础软件迁移评估:4.应 ...

  8. 2015第19本:异类--不一样的成功启示录

    一位移民加拿大的高中同学在2012年回国探亲,聚会时曾推荐了<异类--不一样的成功启示录>这本书,英文书名叫<Outliers - the story of success>, ...

  9. 第五阶段 -- 数据库:day21_07/03/19【事务】

    文章目录 一. 事务的简介 二. 事务的控制 三. 事务的隔离级别 1. 基本概念 2. 事务级别 1. read uncommitted 2 read committed 3. repeatable ...

最新文章

  1. Scrum团队初建的十一件事——Scrum中文网
  2. InfoPath Forms Services的配置
  3. 论文笔记:Group Equivariant Convolutional Networks
  4. 如何用python开发游戏_手把手教你用Python完成一个控制台小游戏-阿里云开发者社区...
  5. linux禁用用户账号,技术|在 Linux 系统中禁用与解禁用户的账号
  6. FreeBSD9.1安装Gnome2桌面
  7. 数学C语言编程,数学规划 (最速下降法,c语言编程).doc
  8. Django:ORM基本操作-CRUD,管理器对象objects,----->删除:删除单个数据,删除批量数据
  9. WordPress解决上传文件大小限制问题
  10. Django项目实战之用户头像上传与访问
  11. 【必修】人工智能原理 学习笔记(二)chapter 2 产生式系统
  12. 信息泄露事件频发 快递行业的隐私面单之战
  13. i5 10400f和r7 2700x选哪个?
  14. 170621 逆向-CrackMe之027
  15. 绩效考核过程中使用系统的必要性
  16. cobaltstrike权限维持
  17. web HTML5新标签对IE低版本浏览器的兼容处理
  18. Stacked autoencoder理论简析
  19. 企业如何确保电子邮件安全?
  20. java实体类转换xml_简单实体类和xml文件的相互转换方法

热门文章

  1. supersocke接收不到数据_基于SuperSocket的北斗终端数据接收服务的设计与实现
  2. dw服务器文件夹在哪里,Dreamweaver CC
  3. 小青蛙oracle跟踪,Oracle 存储过程:游标
  4. @kafkalistener中id的作用_SSM框架(十一):Spring框架中的IoC(1)
  5. mysql索引抽密度_使用python脚本从abaqus输出数据库获取元素密度
  6. 如何查询高考成绩2021年的成绩排位,2020年高考成绩排名怎么查,高考成绩排名是怎么排的...
  7. db2 空值转换函数_Hive常见函数的使用
  8. 机器人总动员中的小草_机器人总动员读后感
  9. java fangfa_daicanfangfa java中的方法 刚入门的分不清带参方法的作用和用处 这个可以详细的讲解如何使用带参方法 - 下载 - 搜珍网...
  10. Springboot分层图解