Store MYSQL connection on a single file
Moderator: General Moderators
Store MYSQL connection on a single file
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.
It is not working
I tried that solution, but it didn't work. my code looks something like this:
and my connection file file.php is something like this :
but it doesn't return any rows. if someone can help me, im kind of new here and in php too.
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());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);with the code you posted you missing a "
try
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.
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());then how are you looping through your data set.
My code was working before, by making the connection directly with:
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.
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());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
<?php echo $file ?>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());