Websites provide information to users. To read more about web technology, checkout this
link. Web pages can be authored using HTML, XML, PHP, Javascript, etc. Information displayed on a webpage may come from a database. There are ways to interface with a database, such as using
PHP Database Interface.
The following are the steps to connect to a database and store or retrieve information:
- Connect to the computer where database server is running using a user ID and password
- Select the database to access (Note that a database system may contain multiple databases)
- Prepare the query in SQL (See Database Technology link for more information)
- Execute the query and obtain the results into a memory location
- Process the results
- Close the database connection
Here is an example:
<?php
mysql_connect('mysql.cstrends.com:1210','root','password'); //Step 1
/* Change the above line depending on your database server address, port number,
user name and password*/
@mysql_select_db('dbName') or die("Unable to select database"); //Step 2
$query="SELECT * FROM MYTABLE;"; //Step 3
$results = mysql_query($query); //Step 4
$norows = mysql_numrows($results); //Step 5
for ($i = 0; $i < $norows; $i++)
{
echo mysql_result($results, $i, 'slno') . "<br/>\n";
}
mysql_close(); //Step 6
?>
References
- PHP/MySQL Introduction from W3Schools
- PHP/MySQL Tutorial
- PHP/MySQL functions
- PHP Documentation
- MySQL Documentation