php 使用pdo鏈接數(shù)據(jù)庫 怎么查詢數(shù)據(jù)庫中的內(nèi)容。查詢出來之后要怎么顯示指定的內(nèi)容。例如,id號,或者name
2 回答
鴻蒙傳說
TA貢獻1865條經(jīng)驗 獲得超7個贊
//打開數(shù)據(jù)庫
$conn = mysql_connect("localhost","root","密碼");
//設置編碼
mysql_query("set names utf8");
//選擇數(shù)據(jù)庫
mysql_select_db("庫名");
//查詢操作
$rs = mysql_query("select * from 查詢語句");
while($row = mysql_fetch_array($rs)){
echo "{$row['字段名']}";
echo "{$row['字段名']}";
};
//釋放結(jié)果集
mysql_free_result($rs);
//關閉數(shù)據(jù)庫
mysql_close($conn);
//
MM們
TA貢獻1886條經(jīng)驗 獲得超2個贊
<?phpheader('content-type:text/html;charset=utf-8');$dsn = 'mysql:dbname=m-test;host=localhost';$user = 'root';//數(shù)據(jù)庫用戶名$passwd = '';//數(shù)據(jù)庫密碼try { $pdo = new PDO($dsn, $user, $passwd); $pdo->query('set names utf8');//設置字符集 $result = $pdo->query('select * from user');//查詢數(shù)據(jù)庫 foreach ($result as $row) { echo $row['id'];//輸出 ID 號 echo ':'; echo $row['name'];//輸出 name echo '<br />'; }} catch (PDOException $e) { echo $e->getMessage();//錯誤信息} ?> |
- 2 回答
- 0 關注
- 219 瀏覽
添加回答
舉報
0/150
提交
取消
