- Code: Select all
<?php
// CHANGE THESE VALUES
// the host (most likely localhost)
$host = 'localhost';
// the username
$user = 'username';
// the password
$pass = 'password';
// the database name
$db = 'some_db';
// the table you want to SELECT data from
$tbl = 'a_table';
// END CONFIG
// Connect to $host with $user and $pass
$link = @mysql_connect($host, $user, $pass);
// Did it fail?
if (!$link) die('Could not connect to database: '.mysql_error()); // Add mysql_error for info
// Select the database $db
if (!@mysql_select_db($db)) die('Could not select database: '.mysql_error());
// query the DB and SELECT all from $tbl
$query = @mysql_query("SELECT * FROM `{$tbl}`");
// Did it fail?
if (!$query) die('Could not query database: '.mysql_error());
echo '<pre>';
// Fetch the rows
while ($row = mysql_fetch_array($query))
{
// dump the $row array info
print_r($row);
}
echo '</pre>';
// Free the $query result
@mysql_free_result($query);
// Close the conn
@mysql_close($link);
/*
Notes:
- the @ before the function names suppresses any warnings/errors. the if (!$<variable>) block checks if the function didn't succeed, and then makes the appropriate errors.
- mysql_error return the last error made by a mysql_* function. It's very useful for debugging.
*/
?>
That's it.
Further references:
PHP Manual section on MySQL
PHP Manual (very useful)



