Home » Categories » Multiple Categories

How do I connect to my MySQL database using PHP?

  1. Open a new file in your text editor
  2. Paste in the following code:

<?php
$dbhost = 'localhost';
$dbuser = '[DATABASE USERNAME]';
$dbpass = '[DATABASE PASSWORD]';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = '[DATABASE NAME]';
mysql_select_db($dbname);
?>

Replace [DATABASE USERNAME] with the name of your database user.  Replace [DATABASE PASSWORD] with the password for your database user.  Replace [DATABASE NAME] with the name of your database.

  1. Save this file as [NAME].php and upload it to your account.

When you want to access the database in any PHP script:

  1. Add the following line to your script:

include '[NAME].php'

  1. The database can now be queried, using standard SQL commands, and the following line:

mysql_query('QUERY')

1 (3)
Article Rating (3 Votes)
Rate this article
  • Icon PDFExport to PDF
Related Articles
How do I increase the memory allocation for my PHP script?
Added on Mon, Mar 16, 2015
How can I upload my MySQL database?
Added on Thu, Feb 26, 2015
How do I enable MIMEMagic on my account?
Added on Mon, Mar 16, 2015
How do I connect to MySQL databases through SSH?
Added on Thu, Feb 26, 2015
How do I use short open tags in my PHP scripts?
Added on Mon, Mar 16, 2015