note 71079 added to function.mysql-connect

From: Date: Fri, 10 Nov 2006 21:59:43 +0000
Subject: note 71079 added to function.mysql-connect
Groups: php.notes 
Request: Send a blank email to php-notes+get-119597@lists.php.net to get a copy of this message
How to get at multiple MySQL databases from PHP while continuing to hide the user credentials in Apache configuration files. (This builds on my solution to the problem of hiding such credentials that I posted in May 2003 at http://uk2.php.net/manual/en/function.mysql-connect.php#32035) <Directory /var/www/html/multidatabase> php_value mysql.default_user "username1 username2" php_value mysql.default_password "secret private" php_value mysql.default_host "localhost server.example.com" </Directory> Note that the quotes are necessary to prevent the parser complaining about seeing too many parameters for php_value. Given this setup in Apache, our script can fetch the composite value $hostnames = @ini_get('mysql.default_host'); Split it into its component parts $hostnames = preg_split("/[\s]+/", $hostnames); Then use the values in this array as if we had hard-coded: $hostnames[0] = "localhost"; $hostnames[1] = "server.example.com" Similar code may be written to fetch the usernames and passwords. (One 'gotcha' with the mysql_error() function is that it will not give a sensible error report if there is a failure to open a second or subsequent connection. It uses the last successfully opened connection as the basis for its message!) ---- Server IP: 66.163.161.117 Probable Submitter: 217.155.67.156 ---- Manual Page -- http://www.php.net/manual/en/function.mysql-connect.php Edit -- https://master.php.net/note/edit/71079 Del: integrated -- https://master.php.net/note/delete/71079/integrated Del: useless -- https://master.php.net/note/delete/71079/useless Del: bad code -- https://master.php.net/note/delete/71079/bad+code Del: spam -- https://master.php.net/note/delete/71079/spam Del: non-english -- https://master.php.net/note/delete/71079/non-english Del: in docs -- https://master.php.net/note/delete/71079/in+docs Del: other reasons-- https://master.php.net/note/delete/71079 Reject -- https://master.php.net/note/reject/71079 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#119597) next »