码迷,mamicode.com
首页 > 数据库 > 详细

mysqli和mysql和pdo查询

时间:2015-03-03 11:14:28      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

 

mysql

mysql_connect($db_host, $db_user, $db_password); 
mysql_select_db($dn_name); 
$result = mysql_query("SELECT `name` FROM `users` WHERE `location` = ‘$location‘"); 
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
{ 
echo $row[‘name‘]; 
} 
mysql_free_result($result);

mysqli

$mysqli = new mysqli($db_host, $db_user, $db_password, $db_name); 
$sql = "INSERT INTO `users` (id, name, gender, location) VALUES (?, ?, ?, ?)"; 
$stmt = $mysqli->prepare($sql); 
$stmt->bind_param(‘dsss‘, $source_id, $source_name, $source_gender, $source_location);//四个参数 所以是dsss整形\字符串 
$stmt->execute(); 
$stmt->bind_result($id, $name, $gender, $location); 
while ($stmt->fetch()) 
{ 
echo $id . $name . $gender . $location; 
} 
$stmt->close(); 
$mysqli->close();

pdo

$dsn = "mysql:host=$db_host;dbname=$db_name"; 
$dbh = new PDO($dsn, $db_user, $db_password); 
$sql = "SELECT `name`, `location` FROM `users` WHERE `location` = ? , `name` = ?"; 
$sth = $dbh->prepare($sql); 
$sth->execute(array($location, $name)); 
$result = $sth->fetch(PDO::FETCH_OBJ); 
echo $result->name . $result->location; 
$dbh = NULL;

mysqli和mysql和pdo查询

标签:

原文地址:http://www.cnblogs.com/jdhu/p/4310343.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!