<?php
/**
* @xwl
* 2014-10-30 09:40
*/
// mysql
//$db = mysql_connect('127.0.0.1','root','root') or die(mysql_error());
//mysql_select_db('test',$db) or die(mysql_error());
//mysql_query("set names utf8",$db);
//$sql = "select * from `posts` where `id`=1 limit 1";
//$data = mysql_query($sql,$db);
//$data = mysql_fetch_array($data,MYSQL_ASSOC); // MYSQL_ASSOC
//var_dump($data);
//pdo
//$db = new PDO('mysql:host=127.0.0.1;dbname=test','root','root');
//$sql = "select * from `posts` where `id`=1 limit 1";
//$data = $db->prepare($sql);
//$data->execute();
//$data->setFetchMode(PDO::FETCH_ASSOC); // FETCH_ASSOC
//$data = $data->fetch();
//var_dump($data);
//pdo sqlite
//$db = new PDO('sqlite:test.sqlite');
//$sql = "select * from `posts` where `id`=1 limit 1";
//$data = $db->prepare($sql);
//$data->execute();
//$data->setFetchMode(PDO::FETCH_ASSOC);
//$data = $data->fetch();
//var_dump($data);
//pdo oracle
//$db = new PDO('oci:dbname=127.0.0.1/test', 'root','root');
//pdo sqlserver
//$db = new PDO('sqlsrv:Server=127.0.0.1;Database=test','root','root');
//pdo execute
$db = new PDO('mysql:host=127.0.0.1;dbname=test','root','root');
$param = '10 or 1';
$sql = "select * from `posts` where `id`={$param} limit 1";
$data = $db->prepare($sql);
$data->execute();
//$sql = "select * from `posts` where `id`=? limit 1";
//$data = $db->prepare($sql);
//$data->execute(array($param));
$data->setFetchMode(PDO::FETCH_ASSOC);
$data = $data->fetch();
var_dump($data);
php使用pdo操作数据
最新推荐文章于 2019-07-23 20:40:57 发布