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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problems connecting in authentication script

Status
Not open for further replies.

PushCode

Programmer
Joined
Dec 17, 2003
Messages
573
Location
US
I'm using the code from the SitePoint.com forum article "Managing Users with PHP Sessions and MySQL". For some reason I am unable to connect to my database. After I type in the username and password and click login, the page shows the line "The site database is unavailable.". But I know it IS available, b/c I'm able to login using an old script I have.

Can anyone tell me how my db details should fit into this code so I can properly connect?

Let's say my db details are:
server: sql3.oururl.com
username: admin
password: pw
database name: ffdb1
Code:
<?php // db.php 

$dbhost = "localhost"; 
$dbuser = "user"; 
$dbpass = "password"; 

function dbConnect($db="") { 
global $dbhost, $dbuser, $dbpass; 

$dbcnx = @mysql_connect($dbhost, $dbuser, $dbpass) 
or die("The site database appears to be down."); 

if ($db!="" and !@mysql_select_db($db)) 
die("The site database is unavailable."); 

return $dbcnx; 
} 
?>
 
You're not getting enough error data back from the script to debug.

Try changing your code so that it tells you why something failed, not just that it did.

At a minimum, I would change:

$dbcnx = @mysql_connect($dbhost, $dbuser, $dbpass) or die("The site database appears to be down.");

to:

$dbcnx = @mysql_connect($dbhost, $dbuser, $dbpass) or die('Connecting:' .mysql_error());


and


if ($db!="" and !@mysql_select_db($db))
die("The site database is unavailable.");

to

if (!@mysql_select_db($db))
die('Selecting:' . mysql_error());


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
How are you calling this function?

It might be beneficial for you to remove the @ character and utilize <? echo mysql_error(); ?> for debugging purposes after the call to mysql_connect and mysql_select_db.

*cLFlaVA
----------------------------
A polar bear walks into a bar and says, "Can I have a ... beer?"
The bartender asks, "What's with the big pause?
 
Great suggestions. Also, how about my db details? Where do I put them in this code?
 
Ideally, your database variables should reside in a global php include. I usually create a file called global.inc or global.php and save it into my includes folder.

<?
//global.inc
define ("DB_NAME", "ffdb1");
define ("HOST_NAME", "localhost");
define ("USER_NAME", "clflava");
define ("PASSWORD"), "password");
?>

Then, on the top of every script I write, I include the file

<?
require_once("includes/global.inc");
?>

Then, I can change these only once when they need to be changed. You can refer to these as constants (because that's what they are, by simply refering to them as USER_NAME, PASSWORD, etc, without the $ in front.

*cLFlaVA
----------------------------
A polar bear walks into a bar and says, "Can I have a ... beer?"
The bartender asks, "What's with the big pause?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top