PHP Database Connections

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I am new to PHP.  I am trying to develop a web site on a development
machine that needs to access data in a mysql database.  I have mysql
running on my development machine with a copy of the database.  While
developing the app. I can connect to the local version of the database
without problems.  Once I upload the database to its host site I need
to change the php scripts that connect to the database to refelect the
appropriate connection associated with the hosting server.  Does
anyone know of an efficient way to have my script test for which
location it is on and connect to the appropriate mysql database?  Or,
does anyone know of a php script that can handle this
programmatically?  Thanks for any time in advance.

A. Cate

Re: PHP Database Connections (A Cate) wrote:

Quoted text here. Click to load it

I put code in all the relevant scripts to test the URL, which is
different between the development and production systems.  All database
access is done in a single class that's included in various files that
need access to the database.  That way, I can make one change to the
class and it changes everywhere.

If you don't have programming skills, start reading some basic books on
programming design that discuss larger projects.  Such practices like
putting things in header files, reusability, etc. are key to making
something easy to modify and maintain.  Since I already have programming
experience, I found the O'Reilly book useful and the PHP Anthology to be
very useful to discuss OOP: / /

DeeDee, don't press that button!  DeeDee!  NO!  Dee...

Re: PHP Database Connections

This is what I do....

I have a file called which resides outside the web root on both my
local PC and my web host.

<-- start
$dbhost      = "localhost";

if ($_SERVER['SERVER_NAME'] == 'localhost') {
   // this is for my PC
   $dbusername = "****";
   $dbuserpass = "****";
   $dbprefix   = null;
} else {
   // this is for my web host
   $dbusername = "********";
   $dbuserpass = "********";
   $dbprefix   = "********";
} // if
<-- end

This sets up different values depending on which location it is being run

$dbprefix is there because on my web host all my database names are prefixed
with my account name.

Tony Marston

Quoted text here. Click to load it

Site Timeline