Friday, June 22, 2012

How to Connect to a MySQL database in PHP

I typically create a file that includes the db connection settings.  So you don't have to repeat that each time you just add "include "dbinfo.php";

dbinfo.php would include something like this:

<?php

//this is hostname or IP, username, password
$con = mysql_connect("localhost","root","root");

if (!$con)

  {

  die('Could not connect: ' . mysql_error($con));

  }

mysql_select_db("yourDBname") or die(mysql_error());

?>

ShareThis