PDA

View Full Version : mysql_pconnect() and .my.cnf


stan
05-17-1999, 06:34 PM
A question on the username / password when connecting to
MySQL from PHP. I now use:

mysql_pconnect("localhost", $db, $pw);

I would, however, like to use

mysql_pconnect("localhost");

where the username / password is read from one location like[nbsp][nbsp]the ~/.my.cnf file is used by the mysql command line tool.

This would make the PHP scripts easier to maintain (no changes when the password is changed), and it would make it less easier to find the username / password for the database.

Any suggestions?

Stan

Justin
05-17-1999, 07:03 PM
The way I did it was I created a variables file that gets included whenever the database needs to be used. Inside of it is:


<?
# Variables for database

# Host Name
$db_host = &quot;localhost&quot;;

# Database Name
$db_name = &quot;xhostfacts&quot;;

# User Name
$db_user = &quot;root&quot;;

# Password
$db_password = &quot;*****&quot;;

# table name
$db_table = &quot;web_host_packages&quot;;

#########################
# Connect to the database
$sqlnum = mysql_connect ($db_host, $db_user, $db_password);

if ($sqlnum < 1) {
[nbsp][nbsp][nbsp][nbsp][nbsp]print &quot;<span class=main>Can't connect to $db_host</span>\n&quot;;
[nbsp][nbsp][nbsp][nbsp][nbsp]print &quot;

\nMake sure all of your variables are correct.&quot;;
[nbsp][nbsp][nbsp][nbsp][nbsp]exit;
} else {
[nbsp][nbsp][nbsp][nbsp][nbsp]@mysql_select_db ($db_name);
}
?>


This has the variables in it and it performs the connection to the database making it easier. Then it's all in one place, making updates easier as well as eliminating the need to connect in every file - it's just done in the include file, so to perform a query I just say:


include &quot;header.php3&quot;;
include &quot;vars.php3&quot;;
mysql_query(&quot;select * from $db_table where blah=blah&quot;);
.
.
.


Let me know if this helps.

PS - username 'root' is only on my localhost hehe - not on the server :)

------------------
Justin Nelson
FutureQuest Support

stan
05-17-1999, 07:54 PM
Thanks.

That certainly solves the maintainability aspect of my question.

Stan