您的位置:首页 > 编程语言 > PHP开发

PHP学习 PDo查询数据库

2014-03-13 22:35 274 查看
<?php
try {
$pdo=new PDO("mysql:host=localhost;dbname=xsphpdb", "root", "123456");
}catch(PDOException $e){
echo $e->getMessage();
}

//获取结果   fetch() fetchAll();

$stmt=$pdo->prepare("select id, name, price, num, desn from shops where id > :id order by id");

$stmt->bindColumn("id", $id, PDO::PARAM_INT);
$stmt->bindColumn("price", $price);
$stmt->bindColumn("name", $name, PDO::PARAM_STR);
$stmt->bindColumn(4, $num);
$stmt->bindColumn(5, $desn);

$stmt->execute(array(":id"=>100));

echo '<table border=1 width=900 align="center">';

echo '<tr>';
for($i=0; $i<$stmt->columnCount(); $i++){
$field=$stmt->getColumnMeta($i);
echo '<th>'.$field["name"]."</th>";
}

echo '</tr>';

while($stmt->fetch()){
echo '<tr>';
echo '<td>'.$id.'</td>';
echo '<td>'.$name.'</td>';
echo '<td>'.$price.'</td>';
echo '<td>'.$num.'</td>';
echo '<td>'.$desn.'</td>';
echo '</tr>';
}
echo '</table>';

echo "总记录数:".$stmt->rowCount()."<br>";
echo "总字段数:".$stmt->columnCount()."<br>";
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: