Connection to MySQL database from PHP
Posted: Wed Feb 02, 2011 5:13 pm
Hi, how do I connect to a MySQL database using PHP so it does not need to be set in the MRT file? The file moves from local to production and each environment has different usernames and passwords.
I am including my database connection php at the top of the page which includes (actual data has been changed):
// Database Connection
$dbtype = "mysql";
$dbhost = "localhost";
$dbname = "database";
$dbuser = "username";
$dbpass = "password";
mysql_connect($dbhost,$dbuser,$dbpass);
@mysql_select_db($dbname) or die( "Error #1001.");
Can I link to this connection? or do i have to specify it in the sti_get_connection_string function? I tried that too like this:
function sti_get_connection_string($connection_type, $connection_string)
{
switch ($connection_type)
{
case "StiSqlDatabase": return "Data Source=SERVER\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
case "StiMySqlDatabase": return "Server=".$dbhost.";Database=".$dbname.";Port=3306;User=".$dbuser.";Password=".$dbpass.";"
case "StiOdbcDatabase": return "DSN=MS Access Database;DBQ=D:\NWIND.MDB;DefaultDir=D:;DriverId=281;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;UID=admin;";
case "StiPostgreSQLDatabase": return "Server=localhost;Database=db_name;Port=5432;User=postgres;Password=postgres;";
}
return $connection_string;
}
I'm assuming I have to link to the connection somehow from the MRT file but am unsure how.
Thanks
I am including my database connection php at the top of the page which includes (actual data has been changed):
// Database Connection
$dbtype = "mysql";
$dbhost = "localhost";
$dbname = "database";
$dbuser = "username";
$dbpass = "password";
mysql_connect($dbhost,$dbuser,$dbpass);
@mysql_select_db($dbname) or die( "Error #1001.");
Can I link to this connection? or do i have to specify it in the sti_get_connection_string function? I tried that too like this:
function sti_get_connection_string($connection_type, $connection_string)
{
switch ($connection_type)
{
case "StiSqlDatabase": return "Data Source=SERVER\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
case "StiMySqlDatabase": return "Server=".$dbhost.";Database=".$dbname.";Port=3306;User=".$dbuser.";Password=".$dbpass.";"
case "StiOdbcDatabase": return "DSN=MS Access Database;DBQ=D:\NWIND.MDB;DefaultDir=D:;DriverId=281;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;UID=admin;";
case "StiPostgreSQLDatabase": return "Server=localhost;Database=db_name;Port=5432;User=postgres;Password=postgres;";
}
return $connection_string;
}
I'm assuming I have to link to the connection somehow from the MRT file but am unsure how.
Thanks