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
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.
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 |