MySQL Forums :: PHP :: Connecting to MySQL on numerous pages


Advanced Search

Connecting to MySQL on numerous pages
Posted by: Mike C-boom ()
Date: March 11, 2012 11:31AM

Hi,

Learning PHP and MySQL...I have a test website that I am building that includes a query to the same MySQL database in pretty much every page. As a result, the following code is part of my new page template.

What I don't know is if there is a more prefered method of handling this connection. Am I doing this right or can I do it better? Looking forward to any comments/discussion. This has been bugging me for quite some time now.

-Mike

<?php

if (!extension_loaded('mysql')) {
if (!dl('php_mysql.dll')) {
echo "Failed to load dynamic library php_mysql.dll
exit;
}
}
$username = "username";
$password = "password";
$hostname = "hostname.isp.com";
$dbh = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL.");
//print "Connected to MySQL<br>";
$selected = mysql_select_db("mysql_db",$dbh)
or die("Could not select mysql_db");

//SOME MySQL Query
//SOME Return From Query and post to website

mysql_close($dbh);
?>

Options: ReplyQuote


Subject Written By Posted
Connecting to MySQL on numerous pages Mike C-boom 03/11/2012 11:31AM
Re: Connecting to MySQL on numerous pages Peter Brawley 03/11/2012 01:04PM
Re: Connecting to MySQL on numerous pages Mike C-boom 03/11/2012 03:01PM
Re: Connecting to MySQL on numerous pages Rick James 03/16/2012 09:37AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.