Page 1 of 1
Store MYSQL connection on a single file
Posted: Wed Jun 08, 2005 5:43 pm
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.
Posted: Wed Jun 08, 2005 6:13 pm
by hawleyjr
Just call the php file via require_once().
It is not working
Posted: Thu Jun 09, 2005 11:05 am
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.
Posted: Thu Jun 09, 2005 11:09 am
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.
Posted: Thu Jun 09, 2005 11:21 am
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.
Posted: Thu Jun 09, 2005 11:39 am
by phpScott
when you do
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());
Posted: Thu Jun 09, 2005 1:33 pm
by hawleyjr
You use mysql_connect() and mysql_pconnect() which one is giving the error?