473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

A. Cate
Jul 17 '05 #1
2 2109
In article <8b*************************@posting.google.com> ,
ac*****@yahoo.com (A Cate) wrote:
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


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...

Jul 17 '05 #2
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

http://www.tonymarston.net

"A Cate" <ac*****@yahoo.com> wrote in message
news:8b*************************@posting.google.co m...
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

Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mudge | last post by:
Hi, My hosting provider only allows me to use 50 connections to my MySQL database that my Web site will use. I don't know what this 50 connections means exactly. Does this mean that only 50...
4
by: dustin lee | last post by:
Over the years I've gotten out of the habit of explicitly closing file objects (whether for reading or writing) since the right thing always seems to happen auto-magically (e.g. files get written...
3
by: Martin B | last post by:
Hallo! I'm working with C# .NET 2.0, implementing Client/Server Applications which are connecting via Network to SQL-Server or Oracle Databases. To stay independent from the underlaying Database...
35
by: Terry Jolly | last post by:
Web Solution Goal: Have a global database connection Why: (There will be 30+ tables, represented by 30+ classes) I only want to reference the database connection once. I put the connection...
4
by: Sierra | last post by:
Problem: Database connections are not being reused properly. SP_WHO2 shows upwards of 200 connections being created per page request. Most connections exist for 60 seconds then close without...
7
by: Martin | last post by:
Hello Does anyone know if it is possible to use the smo db.Create() method to create a new sql server database with sql server 2005 express? The debugger complains that remote operations are not...
5
by: John | last post by:
I have an ASP.NET 2.0 application developed in VB.net, that accesses an Microsoft Access database. When the database is on the same IIS server all works just fine. BUT when it tried to access the...
5
by: Usman Jamil | last post by:
Hi I've a class that creates a connection to a database, gets and loop on a dataset given a query and then close the connection. When I use netstat viewer to see if there is any connection open...
8
by: BD | last post by:
I am developing C# win form app to work with remote database on SQL Server 2005. Problem scenario is as follows: 1. a form is open that has downloaded dataset to local cache 2. computer is...
9
by: Gordon | last post by:
I want to add a feature to a project I'm working on where i have multiple users set up on my Postgres database with varying levels of access. At the bare minimum there will be a login user who...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.