Home >Backend Development >PHP Tutorial >Simple how to use PDO in PHP5.2, how to use php5.2pdo_PHP tutorial

Simple how to use PDO in PHP5.2, how to use php5.2pdo_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:56:23794browse

A simple way to use PDO in PHP5.2, a way to use php5.2pdo

The example in this article describes the simple way to use 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)
);

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;>

Readers who are interested in more PHP-related content can check out the special topics of this site: "Summary of PHP office document operation skills (including word, excel, access, ppt)", "Summary of PHP date and time usage", "php-oriented "Introduction Tutorial on Object Programming", "Summary of PHP String Usage", "Introduction Tutorial on PHP MySQL Database Operation" and "Summary of Common PHP Database Operation Skills"

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

Articles you may be interested in:

  • How to simply use PDO in the database abstraction layer in php
  • Detailed explanation of the PDO class in PHP
  • PDO in php Methods to implement database additions, deletions, modifications and queries
  • PHP uses PDO to connect to the ACCESS database
  • Detailed explanation of the use of PDO, the mysql connection method in php
  • Comparative analysis of pdo and mysqli database connection methods in php
  • Summary of some understanding of PHP PDO
  • Detailed explanation of how php uses PDO
  • How php uses pdo to connect and query sql database
  • php uses pdo to connect and report errors Solution to Connection failed SQLSTATE
  • PHP implements mysql database operation class of PDO
  • The difference between PHP PDOStatement object bindpram(), bindvalue() and bindcolumn
  • PHP PDO Operation summary
  • How to install PhpDocumentor 2 and generate API documentation

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1113697.htmlTechArticleSimple method of using PDO in PHP5.2, how to use php5.2pdo. This article describes PDO in PHP5.2 with examples. Easy to use. Share it with everyone for your reference, the details are as follows: 1. PDO configuration...
Statement:
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