Store MYSQL connection on a single file

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
anfion
Forum Newbie
Posts: 4
Joined: Tue Jun 07, 2005 8:47 am

Store MYSQL connection on a single file

Post by anfion »

I'm trying to figure out how to make MySQL queries and connections to the MYSQL by storing the server, user, password and database on a single .php file, so that when i change my pages from one server to another i have just to change the data on that connection single-file instead of doing that for all my php files.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Just call the php file via require_once().
anfion
Forum Newbie
Posts: 4
Joined: Tue Jun 07, 2005 8:47 am

It is not working

Post by anfion »

I tried that solution, but it didn't work. my code looks something like this:

Code: Select all

require_once('file.php');
$query = "SELECT * FROM table;

	mysql_select_db($database, $file);
	$allin = mysql_query($query, $file) or die(mysql_error());
and my connection file file.php is something like this :

Code: Select all

$hostname = "localhost";
$database = "myDB";
$username = "myUser";
$password = "myPass";
$file = mysql_pconnect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR);
but it doesn't return any rows. if someone can help me, im kind of new here and in php too.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

with the code you posted you missing a "

try

Code: Select all

require_once('file.php');
$query = "SELECT * FROM table";
 
    mysql_select_db($database, $file);
    $allin = mysql_query($query, $file) or die(mysql_error());
try echo'ing out $file to make sure it has the values you are lookin for. check out eval() on php.net

then how are you looping through your data set.
anfion
Forum Newbie
Posts: 4
Joined: Tue Jun 07, 2005 8:47 am

Post by anfion »

My code was working before, by making the connection directly with:

Code: Select all

mysql_connect("server", "user", "pass") OR DIE (mysql_error());
    
	// select the db
	mysql_select_db ("myDB") OR DIE ("Unable to select db". mysql_error());
and then making the query or inserts. but now i tried to make the connection from a file containing passwords for easy changing passwords and usernames. and now it doesn't work, i kind of guided from the way that Macromedia Dreamweaver makes the connection that way but it just won't work.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

when you do

Code: Select all

<?php echo $file ?>
what do you get.
I would have though something about being a resource with a number attached.

or
in your data.php file

Code: Select all

$hostname = "localhost";
$database = "myDB";
$username = "myUser";
$password = "myPass";
mysql_connect($hostname, $username, $password") OR DIE (mysql_error());
// select the db
mysql_select_db ($database) OR DIE ("Unable to select db". mysql_error());
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

You use mysql_connect() and mysql_pconnect() which one is giving the error?
Post Reply