Go Back   CodingForums.com > :: Server side development > PHP

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 11-30-2012, 08:10 PM   PM User | #1
dambuster
New to the CF scene

 
Join Date: Dec 2010
Location: Birmingham (UK)
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
dambuster is an unknown quantity at this point
test to live with mysql_connect_db

Some advice please.

I have a testing server at localhost, after making amendments fixing bugs etc. I upload to a hosted web server. Problem is I have to change all the mysql_connect_db statements before uploading due to parameter 1 of the mysql_connect_db statement on the testing server being set to "localhost" and on the live server it's info provided by the web hosting company.

Is there any way I can set something in my localhost testing environment (or a completely different method) that simulates the same as the live environment
dambuster is offline   Reply With Quote
Old 11-30-2012, 08:17 PM   PM User | #2
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
I see what you mean and I found this hassle. How many times are you calling that function? Do you have multiple databases or just one?

Just upload your local version. When it is live, go into your host directories and manually edit all the connection stuff. Then you've only done it once and you also keep a copy of your original localhost versions of the files. Once you got a live version, you can work locally, then just copy paste the code into your live version by going into the bit where you upload and editing the file which is online directly.

Hope it helps.

Maybe someone can give you better information and good tips to keep in mind. This is what I do but I'm no expert.

Regards,

LC.
LearningCoder is offline   Reply With Quote
Old 12-01-2012, 12:11 AM   PM User | #3
Junsee
New Coder

 
Join Date: Jun 2012
Posts: 42
Thanks: 4
Thanked 5 Times in 5 Posts
Junsee is an unknown quantity at this point
Poeple do this is several ways, personally I use a include file, while other use a function call.


it goes something like this, you just edit file to which host its on.
file 'db.php'
PHP Code:
$conn mysql_connect("localhost""root""") or die("Could not Connect");
$rs mysql_select_db("Database"$conn) or die("Couldn't Find Database");

#$conn = mysql_connect("livehost", "liveroot", "") or die("Could not Connect");
#$rs = mysql_select_db("Database", $conn) or die("Couldn't Find Database"); 
File with the selects and whatever
PHP Code:
include 'db.php';
$sql "SELECT * FROM `table` ; ";
$rs mysql_query($sql$conn) or die ("error with sql query ".$sql);
while (
$row mysql_fetch_array ($rs)){
    
#CODE


Last edited by Junsee; 12-01-2012 at 12:16 AM.. Reason: edit
Junsee is offline   Reply With Quote
Old 12-01-2012, 12:47 AM   PM User | #4
master3w
New to the CF scene

 
Join Date: Dec 2012
Location: India
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
master3w is an unknown quantity at this point
Keep one configuration file name config_db.php or any name when you want to connect database to outer pages just include it
config_db.php

$con=mysql_connect('localhost','root','pass')or die("Could not Connect");
$select=mysql_select_db('database name',$con);




after you test your site using localhost before upload it to server just change config_db.php values localhost, root,pass and database name


Last edited by master3w; 12-01-2012 at 12:50 AM..
master3w is offline   Reply With Quote
Old 12-01-2012, 11:33 PM   PM User | #5
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Just a suggestion, but I use a constant named LOCAL and just set it to true or false to switch between localhost and live - saves having to edit, or comment out, all the other details:

PHP Code:
<?php
if (!defined('LOCAL')) {
    
define('LOCAL'true);      // set this to true or false
}
if (!
defined('DB_USER')) {
    if (
LOCAL) {
        
DEFINE ('DB_USER''Andrew');
        
DEFINE ('DB_PASSWORD''Password1');
        
DEFINE ('DB_HOST''localhost');
        
DEFINE ('DB_NAME''Andy2');
    } else {
        
DEFINE ('DB_USER''user2');
        
DEFINE ('DB_PASSWORD''Password2');
        
DEFINE ('DB_HOST''www.some.com');
        
DEFINE ('DB_NAME''db2');
    }
}
?>
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Users who have thanked AndrewGSW for this post:
LearningCoder (12-01-2012)
Old 12-02-2012, 12:58 AM   PM User | #6
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
What a great idea, would save A LOT of messing about.

Regards,

Lc.
LearningCoder 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 08:36 AM.


Advertisement
Log in to turn off these ads.