Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-24-2011, 09:39 PM   PM User | #1
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,496
Thanks: 44
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
MySQL Connection

Hi

I've been using this code I wrote for a couple of years now and it has been faultless. It uses MySQL not MySQLI so it is a bit on the old side but I figured that many of you may find this useful and adapt as needed even if you only use the principle of it and not the actual code.

First let me explain why I created this. I found that everytime I'd upload my project to the web, I'd forget to change the mysql login details so I'd have to re-edit the mysql code, upload, change it back for my localhost/wamp.

So, I figured that by making use of the $_SERVER['HTTP_HOST'] variable, I could use a technique similar to phpmyadmins config.inc.php file. The code below allows you to configure multiple mysql server connections. When you edit your project locally and then upload everything, you don't need to worry about reconfiguring the mysql connection details. If you have 6 sites, you can share this one file with all of them OR have a seperate version for each configured with the local and remote details. Just be sure not to share the file with your mysql details with anyone else that shouldn't have them.

To connect to mysql in your script you simply do this:
PHP Code:
//Require the file
require_once('mysql.php');

//Open connection and select DB
$MySQL run_mysql($_SERVER['HTTP_HOST']);

//Or
run_mysql($_SERVER['HTTP_HOST']);

//Need to debug this mysql connection? - Set the second parameter to true.
run_mysql($_SERVER['HTTP_HOST'], true); 
That will allow your script to automatically select the correct mysql login details and then connect and select the database.

Below is the actual code, note that each server configuration can have multiple network names that it will recognise - mysql.php:
PHP Code:
<?php
function load_mysql()
   {
   
//Set Mysql connections here:
   //MySQL Server 1 - Localhost
   
$Mysql['Name'] = array('127.0.0.1''localhost''<computer_name>''192.168.0.1');
   
$Mysql['Host'] = 'localhost';
   
$Mysql['User'] = 'root';
   
$Mysql['Pass'] = 'password';
   
$Mysql['DB'] = 'your_database_name';
   
$Connections[] = $Mysql;
   
   
//MySQL Server 2 - The website
   
$Mysql['Name'] = array('www.your-site.com''yoursite.com''sub.your-site.com');
   
$Mysql['Host'] = 'localhost';
   
$Mysql['User'] = 'your-site-mysql-user';
   
$Mysql['Pass'] = 'password2';
   
$Mysql['DB'] = 'db_name';
   
$Connections[] = $Mysql;

   return 
$Connections;
   }

function 
get_mysql($Name)
   {
   
$Connections load_mysql();

   foreach(
$Connections as $Key => $MySql)
      {
      if (
is_array($MySql['Name']))
         {
         if ((
$Search array_search($Name$MySql['Name'])) !== false)
            {
            return 
$MySql;
            }
         }
      else
         {
         if (
strtolower($Name) == strtolower($MySql['Name']))
            {
            return 
$MySql;
            }
         }
      }
      
    die(
"Database connection for $Name not defined / established.");
   }

function 
run_mysql($Name$Debug false)
   {
   
$Connection get_mysql($Name);

   If (
$Connection != NULL)
      {
      If (
$SQL = @mysql_connect($Connection['Host'], $Connection['User'], $Connection['Pass']))
         {
         
//MySQL Connection Success
         
$Output "Connected to $Connection[Host]<br>\n";

         If (
$Connection['DB'] != '')
            {
            
$Output .= "Database $Connection[DB] supplied<br>\n";

            If (@
mysql_select_db($Connection['DB']))
               {
               
//Table Selection Success
               
$Output .= "Selected $Connection[DB]<br>\n";
               }
            else
               {
               
//Table Selection Failure
               
$Output .= "Database not selected: " .mysql_error() ."<br>\n";
               }
            }
         else
            {
            
$Output .= "Database not supplied<br>\n";
            }
         }
      else
         {
         
//MySQL Connection Failure
         
$Output "Not connected to $Connection[Host]: " .mysql_error() ."<br>\n";
         }
      }

   if (
$Debug)
      {
      print 
$Output;
      }
         
   return 
$SQL;
   }
?>
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:56 AM.


Advertisement
Log in to turn off these ads.