Connecting To A mySQL Database Using PHP

Connecting to a mySQL database is a very important part of any PHP application. mySQL has become an integral part of many PHP applications since it is also free to use. Connecting to a mySQL database only requires a single function call.

<?
$resource = mysql_connect( $server, $username, $password );
?>
  • $resource – The variable that the connection will be stored and passed to the other mySQL functions that are going to be called.
  • $server – The address of the database server.  Remembering that this is relative to the server, not your end machines.  For example, if your database server is located on the same machine as your webserver, this will be “localhost”.  If it is in the same datacenter (or network) it could be an ip address like 10.xxx.xxx.xxx or 192.xxx.xxx.xxx.  Otherwise you can use an entire URL like db.mydatabaseurl.com.
  • $username – Username to use to login to the database server
  • $password – Password to use to login to the database server

Trackbacks/Pingbacks:

  1. Learn PHP Now » Blog Archive » Querying A mySQL Database Using PHP - October 26, 2009

    [...] Querying a mySQL database requires an established connection to a mySQL database.  You can read our article on connecting to a mySQL database here. [...]