0
嗨,我是新來的PDO,我只是學習如何做一個查詢,我讀了很多關於這個無處不在互聯網上,沒有運氣,然後我試了這個,但我仍然想知道這是否是正確的做法?最好的方法是如何做到這一點,因爲當我嘗試時,我沒有任何類型的錯誤,並且任何反應。最好和最安全的方式來回顯結果與pdo
<?php
require_once 'dbconfig.php';
// (echo test) $name_structure='%s';
// (echo test) $title_structure='%s';
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$sql = 'SELECT *
FROM portfolio';
$q = $conn->prepare($sql);
$q->execute(array('%son'));
$q->setFetchMode(PDO::FETCH_ASSOC);
while ($r = $q->fetch()) {
echo sprintf($name_structure, $r['name']);
echo sprintf($title_structure, $r['title']);
echo sprintf($description_structure, $r['description']);
echo sprintf($img_structure, $r['img']);
echo sprintf($project_end_structure, $r['project_end']);
}
} catch (PDOException $pe) {
die("Could not connect to the database $dbname :" . $pe->getMessage());
}
感謝您的精彩迴應,這正是我所需要的!和你寫的很好的指南,因爲我會立即閱讀! –