PHP and MySQL Basics
This is actaully just as easy as pulling multiple rows, because it is basicly the same thing, without the loop. Now we know the query needed to pull data from a database, we simply have to modify it slightly to be specific to the row we want. We will assume that you know the ID of the article you want to display, and that this ID is 3.
<?
// connect to mysql and select database
$db = mysql_pconnect("localhost","USER","PASS");
mysql_select_db("DATABASENAME",$db);
// build the query, select our article (we want the one with the id of 3)
$sql = "SELECT * FROM news WHERE id = 3";
// run the query and set a result identifier to the recordset
$res = mysql_query($sql);
// get an array of the result
$article = mysql_fetch_array($res);
echo '<b>'.$article["posted"].' - Author: '.$article["author"].'</b><br>'.$article["content"].'<br>';
?>
This time we don't need to limit our query to 10 rows and we don't need to order it by any column because we are only selecting one row. We also don't need to loop anything, for the same reason. Other than that the code is pretty similar. just a bit shorter!
Deleting Stuff
Deleting from MySQL works very much the same way selecting does. You simply specify DELETE instead of SELECT. Make sure you specify a WHERE col = value. Just as a SELECT will select every row if you do not tell it to narrow the results with WHERE, a DELETE will do the same thing - only instead of selecting, it will delete them all. Also make sure they column you are using to delete your rows is unique, or you could delete more than one row. In this example the "id" column will always be unique because it is an auto incrementing id, so it's safe to use for deletion.
<?
// connect to mysql and select database
$db = mysql_pconnect("localhost","USER","PASS");
mysql_select_db("DATABASENAME",$db);
// build the query
$sql = "DELETE FROM articles WHERE id = 3";
// run the query
mysql_query($sql);
?>
The above code will delete the article with the id of 3. This will be the same article that we previously selected.
With the code created during this article you should have a very, very, very basic news script, with no security, user identification, or even a layout... But hopefully this has helped you understand the basics and you can build a more impressive script around this or use what you learned to create something new.
