Page 1 of 1
Trouble accessing database
Posted: Wed Mar 21, 2007 10:41 pm
by guitarlvr
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.
mysql_connect:
Code: Select all
// 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() );
select.php:
Code: Select all
<?php
if (isset($_POST['submitted'])) {
require_once ('mysql_connect.php');
echo 'connected';
} else {
?>
<html>
<body>
<form action="select.php" method="post">
<p>email: <input type="text" name="email" size="20" /></p>
<input type="hidden" name="submitted" value="TRUE" />
<input type="submit" value="submit" />
</form>
<?php
}
?>
</body>
</html>
Posted: Thu Mar 22, 2007 3:16 am
by mikeq
what are you trying to do? your post doesn't say
Posted: Thu Mar 22, 2007 11:11 am
by guitarlvr
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.
Posted: Thu Mar 22, 2007 11:35 am
by RobertGonzalez
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.
Posted: Thu Mar 22, 2007 11:43 am
by guitarlvr
I will turn on the php errors. The file that i am including is the database file included above.
Posted: Thu Mar 22, 2007 11:50 am
by RobertGonzalez
Couple of questions for you...
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 (@)?
Code: Select all
<?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() );
?>
Could be something like:
Code: Select all
<?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());
}
?>
Posted: Thu Mar 22, 2007 11:59 am
by guitarlvr
I turned on display_errors and changed my mysql_connect to reflect what you said and it still displays a blank screen.
Posted: Thu Mar 22, 2007 12:07 pm
by guitarlvr
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.
Posted: Thu Mar 22, 2007 12:24 pm
by RobertGonzalez
I would suggest, at the very least, removing error suppression in your app (remove the @'s).
Posted: Thu Mar 22, 2007 12:34 pm
by guitarlvr
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?
Posted: Thu Mar 22, 2007 12:38 pm
by RobertGonzalez
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.
Posted: Thu Mar 22, 2007 12:46 pm
by guitarlvr
Where is the log file on the server? i have never seen one and that would be really helpful.
Thanks again for all your help!
Posted: Thu Mar 22, 2007 12:53 pm
by RobertGonzalez
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).
Posted: Thu Mar 22, 2007 12:59 pm
by guitarlvr
awesome, I'll check it out.
Thanks.