CHAPTER 23 INTRODUCING (My web site) PDO // Retrieve all
CHAPTER 23 INTRODUCING PDO // Retrieve all of the rows $rows = $result->fetchAll(); // Output the rows foreach ($rows as $row) { $sku = $row[’sku’]; $name = $row[’name’]; echo “Product: $name ($sku)
“; } Sample output follows: Product: AquaSmooth Toothpaste (TY232278) Product: HeadsFree Shampoo (PO988932) Product: Painless Aftershave (ZP457321) Product: WhiskerWrecker Razors (KL334899) As to whether you choose to use fetchAll() over fetch(), it seems largely a matter of convenience. However, keep in mind that using fetchAll() in conjunction with particularly large result sets could place a large burden on the system, both in terms of database server resources and network bandwidth. fetchColumn() string PDOStatement::fetchColumn ([int column_number]) The fetchColumn() method returns a single column value located in the next row of the result set. The column reference, assigned to column_number, must be specified according to its numerical offset in the row, which begins at zero. If no value is set, fetchColumn() returns the value found in the first column. Oddly enough, it s impossible to retrieve more than one column in the same row using this method, as each call will move the row pointer to the next position; therefore, consider using fetch() should you need to do so. The following example both demonstrates fetchColumn() and shows how subsequent calls to the method will move the row pointer: // Execute the query $query = “SELECT sku, name FROM product ORDER BY name”; $result = $dbh->query($query); // Fetch the first row first column $sku = $result->fetchColumn(); // Fetch the second row second column $name = $result->fetchColumn(1); // Output the data. echo “Product: $name ($sku)”;
If you are searching for cheap webhost for your web application, please visit MySQL5 Web Hosting services.