0
Using Databases In PHP (ver. 1.0.0)
by: HackerVinoth
Introduction:
This isn't quite a full blown tutorial, I like to think of it as a mini-tutorial. This “mini-tutorial” will cover some of the most commonly used functions. There won't be a background section; we're going to go straight to work. Today, I'm going to discuss (if the title didn't give it away) connecting to and using a database with php. The database will be MySQL because it's just sooo darn popular.
Down To Work:
The first thing we need to do is connect to the database.
mysql_connect("somehost", "username", "password") or die ("Can't connect!");
After we have opened a connection to the database, we then select a database.
mysql_select_db("database_name") or die("Can't select database!");
mysql_query("Some query");
Common queries are SELECT and INSERT For full documentation go to the mysql web site (http://www.mysql.com). Another common php function is mysql_num_rows; if it isn't obvious this gets the number of rows from a query. Here is an example of how it can be used with mysql_query:
$result= mysql_query("SELECT * FROM some_table");
$number_of_rows= @mysql_num_rows($result);
if ($number_of_rows == 0)
{
echo "Sorry there are no rows";
}
else {
echo "Yes! we found some rows!";
}
?>
Another really useful function is mysql_fetch_array, because it gets the rows and puts them in an array that contains the name of the rows. That way instead of having to access each row by number you can do it by name! For example, let's say that our database looked like this:
| User | Password |
| John | afasdfadsfdsf |
| Billy | tla;jrjealjwqsldajf |
| Mitch | pqrtupipripewir |
echo "The users in this database are:
";
$result= mysql_query("SELECT * FROM some_table");
while ($row= mysql_fetch_array($result))
{
$username= $row["User"];
echo "$username
";
}
?>
Now let's cover a couple of functions that actually work with the database. The first is mysql_create_db, don't you just love how the functions are named you can figure out what they do just by looking at the function name, this one obviously creates a database. Here's how to use it:
echo "I am going to try to create a database...
";
if (mysql_create_db("test_database"))
{
echo "Hooray, I've created the database!
";
}
else {
echo "Darn couldn't create the database! because: ";
echo "mysql_error()
";
}
?>
echo "I am going to try to delete a database...
";
$result= mysql_drop_db("test_database");
if (!$result)
{
echo "Darn couldn't I couldn't delete the database!
";
}
else {
echo "Hooray, I've deleted the database
";
}
?>
The next two items aren't functions, rather they are queries that you can use to manage an existing table. The following query will insert data into a database:
echo "I am going to try to insert data into a table...
";
$result= mysql_query("INSERT INTO test_database (username, password) VALUES
(Rahim, adfjaldadfsdaf)");
if (!$result)
{
echo "Darn couldn't I couldn't delete the database!
";
}
else {
echo "Hooray, I've deleted the database
";
}
?>
The next query we've already gone over, I'm just going to add to it; after I'm done you should be able to use it to help create a simple search engine (upcomming tutorial)! For the sake of brevity I'll remove all the extra php stuff and just show you the “meat” of the code.
$result= mysql_query("SELECT name FROM some_table WHERE name=Joe AND
lastname=Sixpack OR lastname=Becker ORDER BY lastname LIMIT 20");











0Awesome Comments!