php pdo fetchall code example

Example 1: pdo fetchall as object

<?php
 $sth = $dbh->prepare("SELECT name, colour FROM fruit");
 $sth->execute();

 $result = $q->fetchAll(PDO::FETCH_OBJ);
 //$result contains an array of stdObjects
 ?>

Example 2: php pdo fetch from db

$dbhost = @mysql_connect($host, $user, $pass) or die('Unable to connect to server');

@mysql_select_db('divebay') or die('Unable to select database');
$search = $_GET['searchdivebay'];
$query = trim($search);

$sql = "SELECT * FROM auction WHERE name LIKE '%" . $query . "%'";



if(!isset($query)){
echo 'Your search was invalid';
exit;
} //line 18

$result = mysql_query($trim);
$numrows = mysql_num_rows($result);
mysql_close($dbhost);

Example 3: php pdo fetch from db

$pdo = new PDO('mysql:host=$host; dbname=$database;', $user, $pass);
$stmt = $pdo->prepare('SELECT * FROM auction WHERE name = :name');
$stmt->bindParam(':name', $_GET['searchdivebay']);
$stmt->execute(array(':name' => $name);

Tags:

Php Example