Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error Connecting to DB

Status
Not open for further replies.

bam720

Technical User
Sep 29, 2005
289
US
Any of my pages trying to connect to the database use this... First two lines on the page (after open php tag)
Code:
include_once('include.php');
connect_to_db();

include.php
Code:
function connect_to_test_db(){
	$conn = mysql_connect(localhost, 'root', '');
	if(!conn){
		die('Could Not connect: '.mysql_error());
	}
	mysql_select_db("news");
}

function connect_to_db(){
	$conn = mysql_connect(Server, 'Username', 'p@55w0rd'); //not my real password
	if(!conn){
		die('Could Not connect: '.mysql_error());
	}
	if(!mysql_select_db("news"))
		die('Error Occured: '.mysql_errno()." ".mysql_error());
}


Right now on each page that uses this I get this error returned:
Error Occured: 1045 Access denied for user 'root'@'localhost' (using password: NO)

As you can see My first Connect_To_Test_DB is for when I am testing on ym local machine and root with no password is how I access it. When its on the server (like now) I use the second function. It looks like its trying to connect with the first function, but dying in the second function. Does anyone have any ideas? The server and initial password should be right because its not dying there. Its not dying until the actual db selection, whihc I have copied and pasted the db name. Thanks in advance.
 
Sorry for the post, without full exhuasting my knowledge base.... To put it in the words of sleipnir214 php.net is a great resource.... For some reason localhost is fine on my old server but now I need at least singleticks '' around Server.
 
localhost would be a string. Unless you're using HEREDOC syntax, you need quotes.

There is a setting that can let you get away with no quotes, but frankly I'm not curious enough to go look. Using the better programming practice of quoting strings seems an obviously more portable solution.


Want the best answers? Ask the best questions! TANSTAAFL!
 
Thanks, I definitely concur on the portability issue, as thats what caused the problem in the first place.I will remeber that one and keep it in mind for future use.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top