PHP Database Connections

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • A Cate

    #1

    PHP Database Connections

    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
    programmaticall y? Thanks for any time in advance.

    A. Cate
  • Michael Vilain

    #2
    Re: PHP Database Connections

    In article <8bab890b.04120 41350.be3df31@p osting.google.c om>,
    acatejr@yahoo.c om (A Cate) wrote:
    [color=blue]
    > 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
    > programmaticall y? Thanks for any time in advance.
    >
    > A. Cate[/color]

    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:

    http://www.oreilly.com/catalog/progphp/
    http://www.sitepoint.com/books/phpant1/

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



    Comment

    • Tony Marston

      #3
      Re: PHP Database Connections

      This is what I do....

      I have a file called db.inc 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
      from.

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

      --
      Tony Marston

      This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL




      "A Cate" <acatejr@yahoo. com> wrote in message
      news:8bab890b.0 412041350.be3df 31@posting.goog le.com...[color=blue]
      >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
      > programmaticall y? Thanks for any time in advance.
      >
      > A. Cate[/color]


      Comment

      Working...