php使用pdo操作数据

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值