Forum Moderators: coopster

Message Too Old, No Replies

Issue with properly accessing MySQL database

Correct login info entered in db_login.php but.....

         

coozeman

1:44 pm on May 12, 2009 (gmt 0)

10+ Year Member



Hello all. I have been having an issue with a site that I have been trying to set up. The issue is that while I have users setup with my database, and I have the correct user and password information in my db_login.php file, I get an error message.

This page should display the completed contents of a form and return a confirmation number. The problem is that eliboatc is not the correct user and not the user that I have entered in the db_login.php file. Not sure what is going on here, since it should be gathering the database login info from the db_login file.

[edited by: coopster at 2:09 pm (utc) on May 12, 2009]
[edit reason] no personal urls please TOS [webmasterworld.com] [/edit]

mvaz

2:05 pm on May 12, 2009 (gmt 0)

10+ Year Member



Welcome Coozeman!
Please post your processing code so we can be of help.

coozeman

2:12 pm on May 12, 2009 (gmt 0)

10+ Year Member



Here is the code from the db_login.php file:

<?php

$db_host = 'localhost';
$db_database = 'eliboatc_vanman';
$db_username = '#*$!x';
$db_password = '#*$!x';

//*******************************************************************************
function vanstand_connect($dbhost, $dbdatabase, $dbusername, $dbpassword)

{

$connection = mysql_connect($dbhost, $dbusername, $dbpassword);

if (!$connection)
{
die ("Cannot connect to database: <br />".mysql());
}

//choosing database
$db_select = mysql_select_db($dbdatabase);

if(!$db_select)
{
die("Cannot select the database: <br \>".mysql_error());
}
}

//******************************************************************************
function restricted($logout)
{
require_once('db_login.php');
if($logout=='logout'){
echo "logout code here";
session_start();
session_unregister();
session_unset();
session_destroy();
echo "session is".session_is_registered();
}else{

session_start();

if (empty($_SESSION['user_id']))
{
if (!isset($_SERVER['PHP_AUTH_USER']) ¦¦ !isset($_SERVER['PHP_AUTH_PW']))
{
header('WWW-Authenticate: Basic realm="Member Area"');
header("HTTP/1.0 401 Unauthorized");
echo "You must enter a username and password.";
exit;
}

//connect to db
vanstand_connect('localhost', 'Vanstand', 'root', '');

$web_username = mysql_real_escape_string($_SERVER['PHP_AUTH_USER']);
$web_password = mysql_real_escape_string($_SERVER['PHP_AUTH_PW']);

$query = "SELECT user_id, username";
$query.= " FROM users WHERE ";
$query.= "username='".$web_username."' AND password='".$web_password."' LIMIT 1";
$result = mysql_query($query);
if(!$result)
{
die("Cannot query: <br />".mysql_error());
}
if (!$result_row = mysql_fetch_array($result, MYSQL_ASSOC))
{
header('WWW-Authenticate: Basic realm="Member Area"');
header("HTTP/1.0 401 Unauthorized");
echo "Your username and password combination was incorrect.";
exit;
}
$_SESSION['user_id'] = $result_row['user_id'];
$_SESSION['username'] = $result_row['username'];

}

if (empty($_SESSION['user_id']))
{
echo "empty";
}
$value = $_SESSION['username'];
//setcookie("MoverCookie",$value);
echo "Welcome <b>".$_SESSION['username'].'</b>.';
mysql_close($connection);
}
}
//**************************************************************************************

function new_info($query)
{
require_once('db_login.php');
// connect to db.

vanstand_connect('localhost', 'Vanstand', 'root', '');

$result = mysql_query($query);

if (!$result)
{
die("Cannot Insert: <br />".mysql_error());
}
;
}
//***************************************************************************************
?>

mvaz

2:24 pm on May 12, 2009 (gmt 0)

10+ Year Member



<?php

$db_host = 'localhost';
$db_database = 'eliboatc_vanman';
$db_username = '#*$!x';
$db_password = '#*$!x';

//*******************************************************************************
function vanstand_connect($dbhost, $dbdatabase, $dbusername, $dbpassword)

{

$connection = mysql_connect($dbhost, $dbusername, $dbpassword);

Your variables and have an underscore: db_host, etc, while the underscore is missing in the function. This could be one of the reasons for not connecting to the db.

coozeman

10:34 pm on May 12, 2009 (gmt 0)

10+ Year Member



Thanks. I tried that, but it doesn't seem to have any effect. Here is the error that keeps popping up:

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'eliboatc'@'localhost' (using password: NO) in /home3/eliboatc/public_html/#*$!XX/thankyou.php on line 96

eliboatc is not a user of the database, so I don't understand why it is trying to use that user when a different user is specified in the db_login.php file.

coopster

11:19 pm on May 12, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You have a mess going on there. Although you have these variables defined:
$db_host = 'localhost'; 
$db_database = 'eliboatc_vanman';
$db_username = '#*$!x';
$db_password = '#*$!x';

You are not using them in your connection function:

function vanstand_connect($dbhost, $dbdatabase, $dbusername, $dbpassword) {

Rather, you are overriding them every time you call your connection function:
vanstand_connect('localhost', 'Vanstand', 'root', '');