mysql_connect ("$servername","$dbuser","$dbpassword");
The above function will return true or false depending on the success of the connection. So we will add message to the above function like this.
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
Mysql_connect once establish the connection the link will be present till the script execution is over. It will close it self once the script execution is over or the function mysql_close() is called.
$servername='localhost';
// username and password to log onto db server
$dbuser='userid';
$dbpassword='password';
// name of database
$dbname='db_name';
////////////////////////////////////////
////// DONOT EDIT BELOW /////////
///////////////////////////////////////
connecttodb($servername,$dbname,$dbuser,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
}
mysql_pconnect ("$servername","$dbuser","$dbpassword");
PHP PDO connection to MySQL Database
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| milind | 01-04-2009 |
| nice working | |
| Craig Misak | 05-08-2009 |
| I think you have a typo.... you define $dbusername='userid' but than further down in your mysql_connect its only $dbuser.. I'm a newbe so maybe there is something else going on | |