Jump to content

not connecting to the DB?


frijole

Recommended Posts

My script is working as it should but when it gets to the point where it is supposed to query the DB, I a connection error. Here is the script:

<?php

//this script registers a new user.

require_once 'dbConnect.php';
require_once 'functions.php';

$error = '';

if ($_POST['register'] == 1) {	//if the form has been submitted

$userName = $_POST['user'];
$pass = $_POST['pass'];
$rePass = $_POST['rePass'];

$query = 'SELECT * FROM users WHERE user_id="$userName"';
$insertUser = 'INSERT INTO users VALUES ($userName, $pass)';

if (empty($userName) || empty($pass) || empty($rePass)) {	//are any of the fields empty

	$error = "All fields must be filled out.";}

elseif ($pass != $rePass) {	//all fileds are filled out, are the values valid?

	$error = "The passwords do not match.";}

elseif (strlen($pass) < 4 || strlen($pass) > 12) {	//check if username is too long or too short

	$error = "You password must be between 4 and 12 characters long.";}

elseif (strlen($userName) < 4 || strlen($userName) > 12) {	//check if username is too long or too short

	$error = "You username must be between 4 and 12 characters long.";}

elseif (preg_replace('/[\w ]/', '', $userName)) {	//check for invalid characters in the username

	$error = "Your username may only contain letters, numbers, or underscores";}

else {	//check if username already exists

	$result = mysql_query($query) or die('Connection Error.');

	if (mysql_num_rows($result) > 0) {

		$error = "That username is already taken.";
		}

	else {	//add user to the database

		if (strlen($error) > 0) {

			showRegisterForm($error);

		} else {

		mysql_query($insertUser) or die('Connection Error.');
		echo 'You have been added! sign-in and enjoy.'; 
		}
		}

} //show form and errors if applicable
}
showRegisterForm($error);

?>

 

and here is my dbConnect.php which deals with the DB, minus the username etc.

<?php 
//Database Settings
define("HOST", "localhost");
define("DBUSER", "******");
define("PASS", "******");
define("DB", "******");

//MySQL Connection
$conn = mysql_connect(HOST, DBUSER, PASS);
if (!conn)
{
	// the connection failed so quit the script
	die('Could not connect !<br />Please contact the site administrator.');
}

$db = mysql_select_db(DB);
if (!db)
{
	// cannot connect to the database so quit the script
	die('Could not connect to database <br /> Please contact the site administrator.');
}
?>

 

I would appreciate any ideas.

Link to comment
https://forums.phpfreaks.com/topic/106121-not-connecting-to-the-db/
Share on other sites

<?php 
//Database Settings
$host = 'localhost');
$dbuser = '******');
$pass = '******');
$db = '******');

//MySQL Connection
$conn = mysql_connect($host, $dbuser, $pass);
if (!conn)
{
	// the connection failed so quit the script
	die('Could not connect !<br />Please contact the site administrator.');
}

$db = mysql_select_db($db);
if (!db)
{
	// cannot connect to the database so quit the script
	die('Could not connect to database <br /> Please contact the site administrator.');
}
?>

All of your mysql_query() statements report a 'Connection Error.' when the query fails -

 

$result = mysql_query($query) or die('Connection Error.');

 

Change all those to something meaningful and add a mysql_error() statement to get mysql to tell you why the query failed.

frijole-replace

or die('Connection Error.');

 

with

 

or die('Connection Error'.mysql_error($conn));

 

LooieENG did make changes, but his variables were incorrect.

 

$host = 'localhost');

$dbuser = '******');

$pass = '******');

$db = '******');

 

Those should not have parantheses.

 

here is what I have now and it is acting as if it was successful, no error. It echoes "You have been successfully added" I don't get it.

 

<?php

//Database Settings
$host = "localhost";
$dbUser = "thinksna";
$pass = "******";
$DB = "thinksna_db";

//MySQL Connection
$conn = mysql_connect($host, $dbUser, $pass);
if (!conn)
{
	// the connection failed so quit the script
	die('Could not connect !<br />Please contact the site administrator.');
}

$db = mysql_select_db($DB, $conn);
if (!db)
{
	// cannot connect to the database so quit the script
	die('Could not connect to database <br /> Please contact the site administrator.');
}

//this script registers a new user.

require_once 'functions.php';

$error = '';

if ($_POST['register'] == 1) {	//if the form has been submitted

$userName = $_POST['user'];
$pass = $_POST['pass'];
$rePass = $_POST['rePass'];

$query = 'SELECT * FROM users WHERE user_id="$userName"';
$insertUser = 'INSERT INTO users VALUES ($userName, $pass)';

if (empty($userName) || empty($pass) || empty($rePass)) {	//are any of the fields empty

	$error = "All fields must be filled out.";}

elseif ($pass != $rePass) {	//all fileds are filled out, are the values valid?

	$error = "The passwords do not match.";}

elseif (strlen($pass) < 4 || strlen($pass) > 12) {	//check if username is too long or too short

	$error = "You password must be between 4 and 12 characters long.";}

elseif (strlen($userName) < 4 || strlen($userName) > 12) {	//check if username is too long or too short

	$error = "You username must be between 4 and 12 characters long.";}

elseif (preg_replace('/[\w ]/', '', $userName)) {	//check for invalid characters in the username

	$error = "Your username may only contain letters, numbers, or underscores";}

else {	//check if username already exists

	$result = mysql_query($query) or mysql_error($conn);

	if (mysql_num_rows($result) > 0) {

		$error = "That username is already taken.";
		}

	else {	//add user to the database

		if (strlen($error) > 0) {

			showRegisterForm($error);

		} else {

		$result = mysql_query($insertUser) or mysql_error($conn);
		echo 'You have been added! sign-in and enjoy.'; 
		}
		}

} //show form and errors if applicable
}
showRegisterForm($error);

?>

Yeah, I noticed that.  ;D

 

Change

 

or mysql_error($conn);

 

to

 

or die("ERROR!".mysql_error($conn);

 

ERROR! does not make a difference, obviously.

 

Also, change this:

 

echo 'You have been added! sign-in and enjoy.';

 

To this:

 

if($result) {

 

echo 'You have been added! sign-in and enjoy.';

}

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.