php pdo retrieve data from database code example
Example 1: php mysql select data pdo
$host = 'localhost';
$user = 'root';
$pw = '';
$dbname = 'database';
$dsn = "mysql:host=$host;dbname=$dbname";
$options = [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'];
$db = new PDO($dsn, $user, $pw, $options);
$db -> setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$qry = 'select * from table;';
$result = $db -> query($qry);
while ($row = $result -> fetch()) {
$id = $row[];
}
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;
}
$result = mysql_query($trim);
$numrows = mysql_num_rows($result);
mysql_close($dbhost);