Trouble accessing database

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Trouble accessing database

Post 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>
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

what are you trying to do? your post doesn't say
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Post by guitarlvr »

I will turn on the php errors. The file that i am including is the database file included above.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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()); 
}
?>
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Post by guitarlvr »

I turned on display_errors and changed my mysql_connect to reflect what you said and it still displays a blank screen.
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I would suggest, at the very least, removing error suppression in your app (remove the @'s).
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Post 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?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Post 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!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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).
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Post by guitarlvr »

awesome, I'll check it out.

Thanks.
Post Reply