Simple method to use PDO in PHP5.2_php tips

WBOY
Release: 2016-05-16 19:55:37
Original
1199 people have browsed it

The example in this article describes the simple use of PDO in PHP5.2. Share it with everyone for your reference, the details are as follows:

1. PDO configuration

1. Make sure the PHP version is 5.2.5 or above
2. Find the Dynamic Extensions extension section in php.ini and remove the semicolon
in front of extension=php_pdo.dll 3. Remove the semicolon in front of the corresponding database PDO extension, such as: extension=php_pdo_mysql.dll

2. Database in the example

CREATE TABLE tablename (
  id mediumint(8) UNSIGNED NOT NULL AUTO_INCREMENT,
  str varchar(50) NOT NULL DEFAULT '''',
  PRIMARY KEY (id)
);

Copy after login

3. Program Example

<&#63;php
$dsn = "mysql:host=localhost;dbname=test";
$user = ''root'';
$passwd = ''123456'';
try{
    $db = new PDO($dsn, $user, $passwd);
}catch (PDOException $e)
{
    echo "链接数据库失败!";
    print "异常信息: ". $e->getMessage() . "<br/>";
    print "异常文件: " . $e->getFile() . "<br/>";
    print "异常行号: " . $e->getLine() . "<br/>";
    exit();
}
//$sql = "INSERT INTO tablename SET str = ''Hello''";
//$count = $db->exec($sql); //返回值为影响的行数
//$sql = "DELETE FROM tablename WHERE str = ''Hello'' LIMIT 1";
//$count = $db->exec($sql); //返回值为影响的行数
//预处理需要查询的SQL语句
//$db->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL); //列名按照原始的方式(字段)
$sql = "SELECT * FROM tablename WHERE id < :id AND str = :string"; //sql语句(参数绑定方式)
$query = $db->prepare($sql); //预处理
//用一组绑定参数执行一遍查询
$query->execute(array('':id''=>1, '':string''=>''Hello'')); //处理语句(参数绑定方式)
//$query->setFetchMode(PDO::FETCH_ASSOC); 关联数组形式(只通过字段名下标访问数组内容)
while($item = $query->fetch(PDO::FETCH_ASSOC)) //循环获取数据
{
    echo $item[''id''].":".$item[''str'']."<br/>";
    //print_r ($item);
}
//用另一组绑定参数,再执行一遍查询
$query->execute(array('':id''<=10, '':string''=>''HelloWorld'')); //处理语句(参数绑定方式)
//$query->setFetchMode(PDO::FETCH_ASSOC); 关联数组形式(只通过字段名下标访问数组内容)
while($item = $query->fetch(PDO::FETCH_ASSOC)) //循环获取数据
{
    echo $item[''id''].":".$item[''str'']."<br/>";
    //print_r ($item);
}
$db = null; //释放数据库链接
&#63;>

Copy after login

Readers who are interested in more PHP-related content can check out the special topics on this site: "Summary of PHP operating office document skills (including word, excel, access, ppt)", "php date Time usage summary", "php object-oriented programming introductory tutorial", "php string (string) usage summary", "php mysql database operation Introductory tutorial " and " Summary of common PHP database operation skills "

I hope this article will be helpful to everyone in PHP programming.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template