Been a while since I wrote this, posting it here.
This tutorial is intended to teach you how to connect to a MySQL server and select a database. In the future, I'll add more parts to it as I learn and remember more about MySQL. It's been a while since I've used it.
Introduction
Using MySQL with PHP and Apache can provide a very robust, dynamic, and powerful combination. PHP goes very well together with MySQL with support built right in in version 4.
It is generally a good idea to keep these following variables set in a separate file which you can include when needed if you need to access the same server/database in your web application:
php: | <?
$server = "localhost";
$databse = "db";
$username = "root";
$password = "";
?> | Doing things that way makes it so you only have to change one line of code instead of several.
Connecting to a Server
Always remember to include the db.php file with the following line in any script which needs to initiate a connection with the server:
php: | <?
include("db.php");
?> | Forgetting to include that line could lead to many errors.
The code for connecting to a server is as so:
php: | $connection = mysql_connect($server, $username, $password) or die("Could not connect to server!"); | Used by the next line for selecting the database. Not always needed unless you really want it, but still recommended to have.
Connects to the database using the server location, username, and password provided in the db.php file.
php: | die("Could not connect to server!") | Informs the user of a database failure and stops the script from throwing out more errors.
Selecting Database
Selecting the database is really easy. Just use the following line of code:
Allows for addressing the database later on.
Selects the database for the connection specified in Connecting to a Server. The $connection part is optional, but it allows for connections to multiple servers if required. If none is specified, the most recent connection is assumed.
php: | die("Can't select database!") | Again added to reduce the errors and make sure more errorous code is not executed.
Executing Statements & Queries
The code for executing queries on the server is very simple. First we must build the query with the following line:
Then we must execute it with the following line:
Allows us to use the result obtained later in our script.
Executes the pre-built query.
php: | die("Could not execute query!") | Again used to reduce errors. If something doesn't go right in a script, you don't want your data being screwed up, right?
An example of this in action adds some data into the table 'users':
php: | $query = "INSERT INTO users VALUES (\'datatoadd\')";
$result = mysql_query($query); | Number of Rows in a Table
This section explains how to obtain how many rows there are in a row.
Firstly, as always, we must connect to the server and select a database:
php: | // Include the db.php file
include("db.php");
// Connect to the server
$connection = mysql_connect($server, $username, $password) or die("Could not connect to server!");
// Select the database
$database = mysql_select_db($db, $connection) or die("Could not select database!"); | Then we must build our query and execute it. php: | // Build the query
$query = "SELECT * FROM users";
// Finally execute the query
$result = mysql_query($query); | We use mysql_num_rows() to find out the number of rows in the result. php: | // mysql_num_rows() can be used to find what we want
$num_rows = mysql_num_rows($result); | Finally we can output the number of rows found for us to see. php: | // Echo out the number of rows
echo $num_rows; | It's just that easy. |