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",$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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/540318.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[转载] [OpenGL] shadow mapping(实时阴影映射)

参考链接&#xff1a; Java中静态函数的阴影(方法隐藏) 转载原创&#xff1a;ZJU_fish1996 http://blog.csdn.net/zju_fish1996/article/details/51932954 source:原文地址 code:点击可以直接下载源代码 1978年&#xff0c;Lance Williams在其发表的论文《Casting cur…

[转载] java synchronized静态同步方法与非静态同步方法,同步语句块

参考链接&#xff1a; Java中的静态方法与实例方法 java synchronized静态同步方法与非静态同步方法&#xff0c;同步语句块 并发编程 线程同步 静态方法锁 非静态方法锁 同步块 进行多线程编程&#xff0c;同步控制是非常重要的&#xff0c;而同步控制就涉及到了锁。 对代…

SpringBoot安装和创建简单的Web应用

SpringBoot安装 方式一&#xff1a; Eclipese->Help->Eclipse Marketplace ->Finde STS -> Install 注意&#xff1a;安装过程中挺慢&#xff0c;而且可能会报错&#xff0c;报错时需要重复以上步骤&#xff08;重新安装STS后&#xff0c;安装进度接之前的继续进行…

[转载] JAVA 堆栈 堆 方法区 静态区 final static 内存分配 详解

参考链接&#xff1a; 在Java中为静态最终static final变量分配值 转载来源&#xff1a;https://blog.csdn.net/peterwin1987/article/details/7571808 Java栈与堆 堆:顺序随意 栈:后进先出(Last-in/First-Out). Java的堆是一个运行时数据区,类的对象从中分配空间。这些…

android学习之-Style样式的定义

这个例子主要是写了配置文件&#xff0c;main.xml <?xml version"1.0" encoding"utf-8"?> <LinearLayout xmlns:android"http://schemas.android.com/apk/res/android" android:orientation"vertical" android:lay…

[转载] JAVA泛型杂谈--擦除,协变,逆变,通配符等

参考链接&#xff1a; Java中的协变返回类型 在《JAVA核心思想》这本书里&#xff0c;关于泛型的章节意外的很多&#xff0c;小小的泛型里其实有很多可以学习的内容&#xff0c;我总结下最近看书的成果。 一. 泛型的好处和应用 最基础的用到泛型的地方无非是在容器里 使用…

ASP.NET Session 详解

[ASP.NET] Session 详解 开发者在线 Builder.com.cn 更新时间:2008-03-23作者&#xff1a;黑暗凝聚力量&#xff0c;堕落方能自由 来源:CSDN 本文关键词&#xff1a; Web开发 ASP session 详解 本文仅代表作者个人观点&#xff0c;正确与否请读者自行研究&#xff01;阅读本文…

[转载] java给对象中的包装类设置默认值

参考链接&#xff1a; Java中的对象类Object 处理方法如下 主要适用于&#xff0c;对象中使用了包装类&#xff0c;但是不能给null需要有默认值的情况 /** * 处理对象中包装类&#xff0c;因为快捷签没有用包装类 * * param object 对象 */ public static void handlePara…

hadoop namenode管理元数据机制

一、简要namenode管理元数据机制&#xff1a; 二、详细namenode管理元数据机制&#xff1a; 三、secondary namenode 合并edits和fsimage&#xff1a; 四、namenode存储元数据细节&#xff1a; 五、checkpoint触发点&#xff1a; 本文转自lzf0530377451CTO博客&#xff0c;原文…

[转载] 多线程详解java.util.concurrent

参考链接&#xff1a; java.lang.Object的灵活性 一、多线程 1、操作系统有两个容易混淆的概念&#xff0c;进程和线程。 进程&#xff1a;一个计算机程序的运行实例&#xff0c;包含了需要执行的指令&#xff1b;有自己的独立地址空间&#xff0c;包含程序内容和数据&#…

BABOK - 企业分析(Enterprise Analysis)概要

描述 企业分析描述我们如何捕捉、提炼并明晰业务需要&#xff0c;并定义一个可能实现这些业务需要的一个方案范围&#xff0c;它包括问题定义和分析&#xff0c;业务案例开发&#xff0c;可行性研究和方案范围定义 目的 明确业务战略需要和目标&#xff0c;并建议方案范围 任务…

6、EIGRP配置实验之负载均衡

1、实验拓扑 2、负载均衡原理 等价负载均衡&#xff1a;默认情况下EIGRP只支持等价负载均衡&#xff0c;默认支持4条线路的等价负载均衡&#xff0c;可以通过show ip protocols 查看&#xff0c;最大可以支持16条线路的等价负载均衡&#xff0c;可以在EIGRP路由进程下通过maxim…

[转载] 详解Java中静态方法

参考链接&#xff1a; Java中的静态类 定义&#xff1a; 在类中使用static修饰的静态方法会随着类的定义而被分配和装载入内存中&#xff1b;而非静态方法属于对象的具体实例&#xff0c;只有在类的对象创建时在对象的内存中才有这个方法的代码段。 注意&#xff1a; 非静态…

[转载] 向集合中添加自定义类型--建议在自定义类型的时候要重写equals方法

参考链接&#xff1a; Java重写equals方法 package com.bjpowernode.t01list; import java.util.ArrayList; /* * 向集合中添加自定义类型 */public class TestList04 { public static void main(String[] args) { ArrayList list new ArrayList(); Student s1 new Stude…

[转载] java重写toString()方法

参考链接&#xff1a; 在Java中重写toString() 前言&#xff1a; 在你兴高采烈的写完一个类&#xff0c;创建测试类时&#xff0c;创建对象&#xff0c;传入参数&#xff0c;调用对象&#xff0c;以为会得到参数值&#xff0c;但突然发现输出的是“ 类名什么东东&#xff1f;&…

haproxy+keepalived实现负载均衡及高可用

HAProxy是一个使用C语言编写的自由及开放源代码软件&#xff0c;其提供高性能性、负载均衡&#xff0c;以及基于TCP和HTTP的应用程序代理。相较与 Nginx&#xff0c;HAProxy 更专注与反向代理&#xff0c;因此它可以支持更多的选项&#xff0c;更精细的控制&#xff0c;更多的健…

[转载] Java中变量与常量

参考链接&#xff1a; Java中的实例变量隐藏 1、变量的定义&#xff1a;定义变量就是要告诉编译器这个变量的数据类型&#xff0c;这样编译器才知道需要分配多少空间给它&#xff0c;以及它能存放什么样的数据。在程序运行过程中空间的值是变化的&#xff0c;这个内存空间就成…

Linux-实用快捷键操作

博文说明【前言】&#xff1a; 本文将通过个人口吻介绍Linux下一些常用的实用快捷键&#xff0c;在目前时间点【2017年6月14号】下&#xff0c;所掌握的技术水平有限&#xff0c;可能会存在不少知识理解不够深入或全面&#xff0c;望大家指出问题共同交流&#xff0c;在后续工作…

iOS技术博客:App备案指南

&#x1f4dd; 摘要 本文介绍了移动应用程序&#xff08;App&#xff09;备案的重要性和流程。备案是规范App开发和运营的必要手段&#xff0c;有助于保护用户权益、维护网络安全和社会秩序。为了帮助开发者更好地了解备案流程&#xff0c;本文提供了一份最新、最全、最详的备…

[转载] Java中静态成员变量,静态代码块,静态内部类何时被初始化?

参考链接&#xff1a; Java中的初始化程序块Initializer Block 关于这个问题&#xff0c;本文不扯理论&#xff0c;直接上代码&#xff0c;通过结果来验证结论&#xff0c;废话少说&#xff0c;测试代码如下&#xff1a; public class StaticTest { public static StaticMem…