The basic steps of performing a query, whether using the mysql
command-line tool or PHP, are the same:
Connect to the database.
Select the database to use.
Build a SELECT statement.
Perform the query.
Display the results.
When connecting to a MySQL database, you will use two
new resources. The first is the link identifier that holds all
of the information necessary to connect to the database for
an active connection. The other resource is the results resource.
It contains all information required to retrieve results from an
active database query's result set.
<?php
$db_host='hostname of database server';
$db_database='database name';
$db_username='username';
$db_password='password';
$connection = mysql_connect($db_host, $db_username, $db_password);
if (!$connection){
die ("Could not connect to the database: <br />". mysql_error());
}
$db_select = mysql_select_db($db_database);
?>
Building a SQL query is as easy as setting a variable to the
string that is your SQL query. Of course, you'll need to use a
valid SQL query, or MySQL returns with an error when you
execute the query. The variable name $query is used, but you
can choose anything you'd like for a variable name.
The SQL query in this example is
SELECT * FROM tbl.
command-line tool or PHP, are the same:
Connect to the database.
Select the database to use.
Build a SELECT statement.
Perform the query.
Display the results.
When connecting to a MySQL database, you will use two
new resources. The first is the link identifier that holds all
of the information necessary to connect to the database for
an active connection. The other resource is the results resource.
It contains all information required to retrieve results from an
active database query's result set.
<?php
$db_host='hostname of database server';
$db_database='database name';
$db_username='username';
$db_password='password';
$connection = mysql_connect($db_host, $db_username, $db_password);
if (!$connection){
die ("Could not connect to the database: <br />". mysql_error());
}
$db_select = mysql_select_db($db_database);
?>
Building a SQL query is as easy as setting a variable to the
string that is your SQL query. Of course, you'll need to use a
valid SQL query, or MySQL returns with an error when you
execute the query. The variable name $query is used, but you
can choose anything you'd like for a variable name.
The SQL query in this example is
SELECT * FROM tbl.