You'd think I've never written a prog. before...

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
URWhatUXpress
Forum Newbie
Posts: 11
Joined: Sat Aug 30, 2003 5:00 pm
Location: Grand Rapids, MI

You'd think I've never written a prog. before...

Post by URWhatUXpress »

I can't seem to figure out what is getting fouled up here. Assume the following:

1. All spelling is correct.

2. The database does indeed exist.

What, then, could possibly be wrong with this code:

Code: Select all

if (!($connection = mysql_connect($hostname, $username, $password)))
				{
						die("Cannot connect to Dannyprose Database.");
				} else {
					//Select Dannyprose Database
					echo "Connection Complete" . "<br>";
					if (!(mysql_select_db("ansdannyprosecom")));
					&#123;
						echo "<b>" . "Failure to connect to DANNYPROSECOM." . "</b>";
					&#125;
I'm getting a successful connect to the server, but not to the database. Now, maybe I am just missing something stupid, but this seems correct to me.

Thank you very much for any help and advice.

| dp |
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

your mysql_select_db should be

Code: Select all

<?
if (!(mysql_select_db("ansdannyprosecom", $connection)));
?>
choppsta
Forum Contributor
Posts: 114
Joined: Thu Jul 03, 2003 11:11 am

Post by choppsta »

I'm not sure, but I think the connection identifier is optional. I think the reason it wasn't working is the ";" at the end of your if line. It should look something like this...

Code: Select all

<?php
if (!($connection = mysql_connect($hostname, $username, $password))) { 
	die("Cannot connect to Dannyprose Database."); 
} 
else { 
	//Select Dannyprose Database 
	echo "Connection Complete<br>"; 
	if (!(mysql_select_db("ansdannyprosecom", $connection))) { 
		echo "<b>Failure to connect to DANNYPROSECOM.</b>"; 
	}
}
?>
On a personal note, that's why I like to format my code with the opening brace on the same line, I find it easier to spot errors like that... (but that's just my opinion ;))
Post Reply