connecting to a mysql database
introduction
a lot of web based applications rely on the use of a database in order to store their data. in this article i will discuss how to connect to a database using php.
the code
connecting to a database is really quite simple in php. you will need to know the following information about the database in order to establish a connection: server, name, username, and password. to make it easy to visualize i prefer to set variables for each and then use the variable names when i make the actual connection call:
<?php $db_name = "my_database"; $db_serv = "localhost"; $db_user = "my_username"; $db_pass = "my_password"; $db_conn = mysql_connect($db_serv, $db_user, $db_pass); mysql_connect($db_name, $db_conn); ?>
that’s it! now that you have established a connection you can make queries to the database to recall data:
<?php $sql = "SELECT * FROM some_table"; $result = mysql_query($sql); ?>
conclusion
its really quite simple and storing information in a database not only makes a site run more smoothly, but it also allows you to create powerful content management options so you can quickly update information on the site without changing anything in the site’s files themselves. this is great for template based sites where the pages stay pretty much the same and the content inside them varies depending on a given combination of variables.




