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

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

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

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
3月前
|
关系型数据库 MySQL API
|
7月前
|
关系型数据库 MySQL PHP
PHP 原生操作 Mysql
PHP 原生操作 Mysql
81 0
|
3月前
|
关系型数据库 MySQL PHP
|
4天前
|
PHP
web简易开发——通过php与HTML+css+mysql实现用户的登录,注册
web简易开发——通过php与HTML+css+mysql实现用户的登录,注册
|
7月前
|
关系型数据库 MySQL 数据库连接
PHP 原生操作 Mysql 增删改查案例
PHP 原生操作 Mysql 增删改查案例
87 0
|
2月前
|
监控 关系型数据库 MySQL
PHP与MySQL的结合:实现局域网上网行为监控软件的数据库管理
在当今信息化时代,网络安全日益成为重要的话题。为了有效监控和管理局域网上网行为,开发一个基于PHP和MySQL的数据库管理系统是一个理想的选择。本文将介绍如何结合PHP和MySQL,开发一款简单而高效的局域网上网行为监控软件,并重点关注数据库管理方面的实现。
184 0
|
4月前
|
NoSQL 关系型数据库 应用服务中间件
Linux安装 OpenResty、Nginx、PHP、Mysql、Redis、Lua、Node、Golang、MongoDB、Kafka等
Linux安装 OpenResty、Nginx、PHP、Mysql、Redis、Lua、Node、Golang、MongoDB、Kafka等
88 0
|
6月前
|
关系型数据库 MySQL PHP
PHP环境搭建(安装MySQL)
PHP环境搭建(安装MySQL)
42 0
|
6月前
|
关系型数据库 MySQL PHP
【Bug解决】Thinkphp5 PDO::__construct(): MySQL server has gone away解决办法
【Bug解决】Thinkphp5 PDO::__construct(): MySQL server has gone away解决办法
|
7月前
|
关系型数据库 MySQL PHP
PHP 原生操作 Mysql 分页数据案例
PHP 原生操作 Mysql 分页数据案例
96 1