MySQL Database
- ID: PHP - 4
- Date Added:2005-12-20
- Category: PHP/My SQL
- Difficulty: Medium
- Author: Rei
What's it?
MySQL is often referred as PHP's partner in crime, and stands for Structured Query Language. Usually when your have PHP access, you probably have Structured Query Language. Through Structured Query Language, you can create databases to store data. These databases store information, for example, in real life, companies use databases to store their employees name, address, and other basic information. This saves a lot of time for the company to sort through the list in case they need to find specific employee, or the ones located in a specific branch. Are you with me so far? In online life, the same database work the same way. You keep records of your layout information, and then you can use the MySQL toggling code to display layouts depending upon the: category (DIV, table, popup, iframe), type (Anime, manga, games), and so forth. In this tutorial, I will assume that your know how to create a database.Connecting
This is the most basic code to connect to your database:<?phpCopy and paste that code onto the page you'd like to connect to the database. This code does not select the database. You can read more about selecting a table from a database in another tutorial.
$host="localhost"; // Most of the time your host will be localhost, however,
it differs from host to host.
$user="stephanie"; // This field if for your database username.
$pass="stephchan"; // Your password for the database.
$dbase="endless_db"; // Your database. Oftentimes, you need the endless_
before placing your database name.
$connection = mysql_connect("$host" , "$user" , "$pass")
// If we can't connect to the database, (error with username, password, or
database name), this message will show up instead.
or die ("Can't connect to MySQL");
$db = mysql_select_db($dbase , $connection)
// If we accidently placed the wrong database name, this message will show up.
or die ("Can't select database.");
?>
Legend
- - the variable which assigns your database host name. Most of the time your host will be
localhost, however, it differs from host to host. - - Your username. Sometimes in order to create a user in a database, you need to include the first half of your database name.
- - Your database password.
- - Your database name. Oftentimes, you need the
- - This will connect to your database with the password, username, and database name provided.
- - If there are problems with the database for example: wrong password/username, typo, etc, this will show up instead.
- - If you provided the wrong database name, you will get this error.
Last Words
This is the very basics of MySQL, and once you understand this tutorial, you've taught yourself how to connect into your MySQL database. To insert a code, simply look for the "Insert" button on PhpMyAdmin. If your host happen to not support MySQL, you can sign up for a free database at http://www.freesql.org/. Keep in mind that you should always backup your databases, just like you backup your site at least ONCE a month, or every time you revamp your site. This prevents loss of information if your server crashed. Also, you can always use some real-life database application such as Microsoft Excel, and ClarisWorks Database to create your database offline, then import it using PhpMyAdmin.« back · clear ¦ reload · forward »
Akatsuki Designs © Rei [2006-2008]. Site, layout, and content © Rei. All rights reserved. Valid CSS and HTML
Fight Spam! | Spot a typing error? | HostTracker
Fight Spam! | Spot a typing error? | HostTracker



