Using PHP to read MySQL

This is probably the most simplest thing to do, but I have no idea how to do it.

I currently have a page which inserts data into an MySQL database, I now want another page to read the last added entry and display it. If I leave a field blank, I then want the script to search for the last added entry.

As I said, it’s probably easy, but I don’t know how to do it. Thanks in advance!

I’ve got something to work, it’s long winded, but better than nothing. I now need it to display the last entry added! Just the last entry added! Here is my code:

[php]<?PHP

$user_name = “slasher_mt”;
$password = “PASSWORD”;
$database = “DATABASE”;
$server = “localhost”;
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);

if ($db_found) {

$SQL = “SELECT * FROM Stats”;
$result = mysql_query($SQL);

while ($db_field = mysql_fetch_assoc($result)) {
print $db_field[‘cscore’] . “
”;
}

mysql_close($db_handle);

}
else {
print "Database NOT Found ";
mysql_close($db_handle);
}

?>[/php]

Currently, this displays ALL of the entries in the database.

It is hard to give advice without knowing your table fields. To identify last entry, you need to have some id field (auto increment), or a date field where you save date/time when record was added to a table.
For example, if you have auto increment field named id, this is the query to get last entry:
[php]$SQL = “SELECT * FROM Stats ORDER BY id DESC LIMIT 1”;[/php]

and the rest of your code can be the same what you have now.

Sponsor our Newsletter | Privacy Policy | Terms of Service