単純なmysqliソリューション:
$db = new mysqli('localhost','user','password','database');
$resource = $db->query('SELECT * FROM table WHERE 1');
while ( $rows = $resource->fetch_assoc() ) {
print_r($rows);//echo "{$row['field']}";
}
$resource->free();
$db->close();
エラー処理あり:致命的なエラーが発生した場合、スクリプトはエラーメッセージで終了します。
// ini_set('display_errors',1); // Uncomment to show errors to the end user.
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$db = new mysqli('localhost','user','password','database');
$resource = $db->query('SELECT field FROM table WHERE 1');
while ( $row = $resource->fetch_assoc() ) {
echo "{$row['field']}";
}
$resource->free();
$db->close();
イテレータの使用:PHP5.4でサポートが追加されました
$db = new mysqli('localhost','user','password','database');
foreach ( $db->query('SELECT * FROM table') as $row ) {
print_r($row);//echo "{$row['field']}";
}
$db->close();
単一のレコードをフェッチする:このコードはループを必要としません。
$db = new mysqli('localhost','user','password','database');
$resource = $db->query('SELECT field FROM table');
$row = $resource->fetch_assoc();
echo "{$row['field']}";
$resource->free();
$db->close();