Tôi đang dần chuyển tất cả các chức năng LAMP websites
của mình từ các chức năng mysql_
sang các chức năng PDO
và tôi đã đạt được bức tường gạch đầu tiên của mình. Tôi không biết làm thế nào để lặp qua kết quả bằng một tham số. Tôi ổn với những điều sau:
foreach [$database->query["SELECT * FROM widgets"] as $results]
{
echo $results["widget_name"];
}
Tuy nhiên, nếu tôi muốn làm điều gì đó như thế này:
foreach [$database->query["SELECT * FROM widgets WHERE something='something else'"] as $results]
{
echo $results["widget_name"];
}
Rõ ràng là 'một cái gì đó khác' sẽ năng động.
Đã hỏi ngày 1 tháng 10 năm 2008 lúc 21:52Oct 1, 2008 at 21:52
Andrew G. Johnsonandrew G. JohnsonAndrew G. Johnson
26.3k30 Huy hiệu vàng88 Huy hiệu bạc134 Huy hiệu đồng30 gold badges88 silver badges134 bronze badges
Dưới đây là một ví dụ để sử dụng PDO để kết nối với DB, để bảo nó ném các ngoại lệ thay vì lỗi PHP [sẽ giúp gỡ lỗi của bạn] và sử dụng các câu lệnh được tham số hóa thay vì tự thay thế các giá trị động vào truy vấn [rất được khuyến nghị]:
// connect to PDO
$pdo = new PDO["mysql:host=localhost;dbname=test", "user", "password"];
// the following tells PDO we want it to throw Exceptions for every error.
// this is far more useful than the default mode of throwing php errors
$pdo->setAttribute[PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION];
// prepare the statement. the placeholders allow PDO to handle substituting
// the values, which also prevents SQL injection
$stmt = $pdo->prepare["SELECT * FROM product WHERE productTypeId=:productTypeId AND brand=:brand"];
// bind the parameters
$stmt->bindValue[":productTypeId", 6];
$stmt->bindValue[":brand", "Slurm"];
// initialise an array for the results
$products = array[];
$stmt->execute[];
while [$row = $stmt->fetch[PDO::FETCH_ASSOC]] {
$products[] = $row;
}
Dharman ♦♦
28.5K21 Huy hiệu vàng77 Huy hiệu bạc129 Huy hiệu đồng21 gold badges77 silver badges129 bronze badges
Đã trả lời ngày 2 tháng 10 năm 2008 lúc 0:24Oct 2, 2008 at 0:24
0
Theo tài liệu PHP được nói rằng bạn sẽ có thể thực hiện như sau:
$sql = "SELECT * FROM widgets WHERE something='something else'";
foreach [$database->query[$sql] as $row] {
echo $row["widget_name"];
}
Dharman ♦♦
28.5K21 Huy hiệu vàng77 Huy hiệu bạc129 Huy hiệu đồng21 gold badges77 silver badges129 bronze badges
Đã trả lời ngày 2 tháng 10 năm 2008 lúc 0:24Oct 1, 2008 at 22:04
Theo tài liệu PHP được nói rằng bạn sẽ có thể thực hiện như sau:Darryl Hein
Đã trả lời ngày 1 tháng 10 năm 2008 lúc 22:0490 gold badges214 silver badges260 bronze badges
3
Darryl Heindarryl Hein
// Wrap a PDOStatement to iterate through all result rows. Uses a
// local cache to allow rewinding.
class PDOStatementIterator implements Iterator
{
public
$stmt,
$cache,
$next;
public function __construct[$stmt]
{
$this->cache = array[];
$this->stmt = $stmt;
}
public function rewind[]
{
reset[$this->cache];
$this->next[];
}
public function valid[]
{
return [FALSE !== $this->next];
}
public function current[]
{
return $this->next[1];
}
public function key[]
{
return $this->next[0];
}
public function next[]
{
// Try to get the next element in our data cache.
$this->next = each[$this->cache];
// Past the end of the data cache
if [FALSE === $this->next]
{
// Fetch the next row of data
$row = $this->stmt->fetch[PDO::FETCH_ASSOC];
// Fetch successful
if [$row]
{
// Add row to data cache
$this->cache[] = $row;
}
$this->next = each[$this->cache];
}
}
Huy hiệu vàng 140K90
Nếu bạn thích cú pháp Foreach, bạn có thể sử dụng lớp sau:
foreach[new PDOStatementIterator[$stmt] as $col => $val]
{
...
}
}Jun 14, 2013 at 6:07
Sau đó để sử dụng nó:John K
Đã trả lời ngày 14 tháng 6 năm 2013 lúc 6:071 gold badge7 silver badges15 bronze badges