MySQL Connect

Once you have created a database, there are a few ways you can connect to it.
To connect from a PHP script, just put this in your file:
1
2
3
4
5
6
7
<?
 
    mysql_connect("DBSERVER", "DBUSERNAME", "DBPASSWORD");
 
    mysql_select_db("DBNAME");
 
    ?>
using the following substitutions for the bold terms as above, plus substituting DBPASSWORD with your own mysql password:
  • Replace DBSERVER with the correct database servername for your site.
  • Replace DBUSERNAME with your own mysql username.
  • Replace DBNAME with your own mysql databasename.
The connection will be closed automatically when the script ends. To close the connection before, use the mysql_close() function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
 
$con = mysql_connect("localhost","peter","abc123");
 
if (!$con)
 
  {
 
  die('Could not connect: ' . mysql_error());
 
  }
 
// some code
 
mysql_close($con);
 
?>
PHP Filter
MySQL Introduction
MySQL Create
MySQL Insert