I am having problems getting my MySQL connection setup. I have two pages "mysql_connect.php" and "select.php" mysql_connect holds the database information and creates the connection. The purpose of select.php is just to input an email address and if the database connects, the screen displays "connected". This is not originally what i was trying to do but I'm trying to troubleshoot the script. I am able to use the mysql quiery browser and run queries successfully with the same login information used in the mysql_connect file. Any help is much appreciated.
// Set the database access information as constants.
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', 'password');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'user');
// Make the connection.
$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error() );
// Select the database.
@mysql_select_db (DB_NAME) OR die ('Could not select the database: ' . mysql_error() );
All I'm trying to do with this is connect to the database and if it is successfull, output "connected" on the screen. When i run this, all i get is a blank screen.
A blank screen is a parse error. Turn display_errors on in your PHP.ini file or check your error logs for where the error is. Or post your code that you are including so we can have a look at it.
Is this app being distibuted? If not, just throw the database connection details in the connection function.
Do you need the database connection details after connecting to the database? If not, spare the defining of the constants.
Why are you suppressing error reports (@)?
<?php
// Set the database access information as constants.
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', 'password');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'user');
// Make the connection.
$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error() );
// Select the database.
@mysql_select_db (DB_NAME) OR die ('Could not select the database: ' . mysql_error() );
?>
<?php
// Make the connection.
if (!$con = mysql_connect('host', 'user', 'password'))
{
die('Could not connect to MySQL: ' . mysql_error());
}
// Select the database.
if (!mysql_select_db('dbname'))
{
die('Could not select the database: ' . mysql_error());
}
?>
I apologize. I was able to get it to work. The mysql extension in my PHP.ini file was commented out. Once i un-commented it and restarted my apache server it works fine.
Thanks for all your help.
Wayne\
EDIT: what i was originally trying to accomplish with this script works now as well. Thanks again.
If the website is in production and you have error supressing off and the user gets an error, isn't it bad if they see your php code? Also, in the database connection code you included above, what does the '!' do in front of $con and mysql_select_db?
guitarlvr wrote:If the website is in production and you have error supressing off and the user gets an error, isn't it bad if they see your php code?
It is always bad if they see your code. But an error does not show them the code, it only shows information about the page the error occurred on. Display errors should be off, however, so they will not even see that. But the server log will still report the error so you can view them later. But you can't see any error information if the errors are hushed up.
guitarlvr wrote:Also, in the database connection code you included above, what does the '!' do in front of $con and mysql_select_db?
'!' is the negation operator. Basically it is saying that if the connection to the database comes back as false (or 0), then execute the die part of the code.
PHP errors are usually stored in the server log. On a *nix install, using Apache, the usual location is /etc/httpd/log/error_log (you can always do a find / -name error_log to locate it if need be). On windows using apache, it will be in the log folder of the Apache folder in Program Files (typically).