Forum Moderators: coopster

Message Too Old, No Replies

mysql local and remote

one script to connect to mysql local and remote

         

stuBCN75

10:58 am on Dec 4, 2003 (gmt 0)

10+ Year Member



ok, here is the question. i am very new to php and mysql. i am running osx on my laptop. i am testing locally with the apache server running php. i also have a mysql db on my shared sever with media temple. ok, no problems so far my site is working fine with php and mysql. i am just about to install mysql locally on my mac. my question is ( sorry its a bit long winded! ) my connection scripts in php all connect to the mysql db on the remote server, but i also want to run the pages locally for testing.

do i need two connect scripts on my pages, one for local one for remote?

if i call the db loaclly the same name and username etc. as the remote mysql db, would that work?

if so, how do i do that? the db locally will mirror the remote db ( not sure how to do that yet! )

i am downloading mysql for osx at the moment, just wanted to check before i go too far!

hope that makes some sense, hope you can help me out, like i said this is all very new to me.

coopster

11:41 am on Dec 4, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, stuBCN75!

If you are asking, can I setup my test server just like the production server and run the same exact scripts without having to modify the mysql_connection information, the answer is Yes, if the server parameter in your mysql_connect [us4.php.net] function for your connection is localhost. Whenever you specify "localhost" or "localhost:port" as server, the MySQL client library will override this and try to connect to a local socket (named pipe on Windows). If you want to use TCP/IP, use "127.0.0.1" instead of "localhost".

I generally create a database connection script that I include when necessary. This way, when developing for different servers, I can simply copy and modify my connection(s) in one script:

database_connection.inc.php

<?php
$db_server = 'localhost';
$db_user = 'userid';
$db_pwd = 'password';
$db_name = 'databasename';
$db_link = @mysql_connect($db_server, $db_user, $db_pwd)
or exit('Could not connect: ' . mysql_error());
$db = @mysql_select_db($db_name, $db_link)
or exit('Could not select database: ' . mysql_error());
?>


Then I simply include this in other scripts when I need to access the database:

some_script.php

<?php
include_once("database_connection.inc.php");
// more code here...
?>

coopster

11:44 am on Dec 4, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> the db locally will mirror the remote db ( not sure how to do that yet! )

Somebody may offer a better solution, but you may want to have a look at mysqldump [mysql.com].

stuBCN75

12:38 pm on Dec 4, 2003 (gmt 0)

10+ Year Member



great, thanks for the help.

so i should not need to change any of my connection scripts, thats great. so when it is running locally it automatically finds the local db and connects with that?

its for my own site, so i only have one connect script i use at the moment.

what about the user-name and password? when the pages are running locally does it ignore them and connect to the local db anyway? or should i set up my local db with the same user-name and password as my remote one?

sorry, if im asking stupid questions? its quite a lot to get your head around. im a designer primarily but want to get my own site running via a db, rather than having to hand code each page. (eatpixels.com) it seems to working fine at the moment. i have tried to look around on the net to find answers before posting, its hard to know what to look!

downloading mysql for osx at the moment ,i only have a 56k connection so its taking for ever. i will let you know how i get on.

many thanks