Jump to content

Login Function Can't Find DB, but Other Functions Can?


Go to solution Solved by mac_gyver,

Recommended Posts

Hi everyone! Thanks, in advance, for any and all help.  It's greatly appreciated.

So I created this function:

function login()
{	
	$db_hostname = "localhost"; 
	$db_username = "root";
	$db_password = "";
	$db_name =     "justinalba_module3_db";
	$dbc = @mysqli_connect($db_hostname, $db_username, $db_password, $db_name)
   	 or die("Unable to connect to MySQL");
	if (isset($_POST['submitLogin'])) 
	{ 
// if form has been submitted
// makes sure they filled it in
 		if(!$_POST['userName'] | !$_POST['passwordLogin']) 
		{
 			die('You did not fill in a required field.');
	 	}
 		$query = mysql_query("SELECT * FROM users WHERE name_username = '".$_POST['userName']."'")or die(mysql_error());
	 	//Gives error if user dosen't exist
		$check = mysql_num_rows($query);
		if ($check == 0) 
		{
 			die('That user does not exist in our database. <a href=add.php>Click Here to Register</a>');
		}
		 while($info = mysql_fetch_array( $query )) 	
		{
 			$_POST['passwordLogin'] = stripslashes($_POST['passwordLogin']);
 			$info['password'] = stripslashes($info['password']);
 			//gives error if the password is wrong
 			if ($_POST['passwordLogin'] != $info['password']) 
			{
 			die('Incorrect password, please try again.');
 			}
			else 
			{ 	
			echo "you are logged in";
			}
		}
	}
}

 

I get an error that there is "No database selected."  However the rest of my functions:

function checkUserName(){
	if(isset($_POST['userNameAvailabilityCheck']))
	{
		$username = $_POST['username'];
		$db_hostname = "localhost"; 
		$db_username = "root";
		$db_password = "";
		$db_name =     "justinalba_module3_db";
		$dbc = @mysqli_connect($db_hostname, $db_username, $db_password, $db_name)
    		 or die("Unable to connect to MySQL");
		$query = "SELECT name_username FROM user WHERE name_username = '$username' ORDER by ID DESC";
		$result = mysqli_query($dbc, $query);
		$row = mysqli_fetch_row($result);			
		$usernameExisting = $row[0];
	 	if($usernameExisting == $username) // if the result matches the first MySql row result, error
			{
				echo 'Username already exists.';
			}
		elseif (strlen($username) < 6 || strlen($username) > 15) // checks length of username
			{
				echo "Username must be 6 to 15 characters";
			}
		elseif (preg_match("/^[a-zA-Z1-9]+$/", $username)) //checks for illegal characters, succcess!
			{
  				echo 'Username is available.';
			}
		else //if there are illegal characters
			{
   				echo 'Use alphanumeric characters only.';
			}
	}
}
function register(){
if(isset($_POST['submitRegistration']))
	$username=$_POST['username'];
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$password=$_POST['password2'];
	{
	if($_POST['username']=="")
		{
			echo "Please type username";
		}
	elseif (!preg_match("/^[a-zA-Z1-9]+$/", $username)) //checks for illegal characters, succcess!
		{
  			echo 'Use alphanumeric characters only.';
		}
	elseif (strlen($username) < 6 || strlen($username) > 15) // checks length of username
		{
			echo "Username must be 6 to 15 characters";
		}
	elseif($_POST['password1']==""|$_POST['password2']=="")
		{
			echo "Please type password";
		}
	elseif($_POST['password1']!=$_POST['password2'])
		{
			echo "Uh-oh.  Your passwords don't match.";
		}
	else
		{	
			$db_hostname = "localhost"; 
			$db_username = "root";
			$db_password = "";
			$db_name =     "justinalba_module3_db";
			$username=$_POST['username'];
			$firstName = $_POST['firstName'];
			$lastName = $_POST['lastName'];
			$password=$_POST['password2'];
			$dbc = @mysqli_connect($db_hostname, $db_username, $db_password, $db_name)
			     or die("Unable to connect to MySQL");
			$sql = "INSERT INTO user (id, name_first, name_last, name_username, password) VALUES 
				('', '$firstName', '$lastName', '$username', '$password')";
			mysqli_query ($dbc, $sql) or die("Problem executing query");
			echo "Rows inserted: ", mysqli_affected_rows($dbc), "<br><br>";

$rowcount = 0;

$q = mysqli_query($dbc, "select * from user");
while ($dbc = mysqli_fetch_row($q)) {
    $rowcount++;

    for ($k=0; $k<count($dbc); $k++){
        echo " $dbc[$k] ";
    }
    echo "<br>";
}

echo "<p> A total of  $rowcount rows<br>";
		}
	}
}

 

Seem to work without a hitch...any suggestions on what the problem with the database is in that particular function when it works through all the others?  My guess is that I'm not passing $dbc through any of the sql statements.  What do you think?

localhost.sql.zip

registration.php

  • Solution

you have a mysql_query in your code. you are trying to use a mysqli database connection. you should be using a mysqli_query.

 

you are repeating code for your mysqli database connection in each function. that will lead to typo and other errors. you should be creating one database connection per page and passing it into any function or class that needs it.

by using a global right?  I've looked all over, but when I put "global $dbc" with the code nested in top Dreamweaver tells me the line with "global" is an error.  Thank you btw, I'm about to put your suggestion action.  I really appreciate your swift reply!

That mostly solved the problem, but I'm still getting an error...

 

function login()
{	
	$db_hostname = "localhost"; 
	$db_username = "root";
	$db_password = "";
	$db_name =     "justinalba_module3_db";
	$dbc = @mysqli_connect($db_hostname, $db_username, $db_password, $db_name)
   	 or die("Unable to connect to MySQL");
	if (isset($_POST['submitLogin'])) 
	{ 
// if form has been submitted
// makes sure they filled it in
 		if(!$_POST['userName'] | !$_POST['passwordLogin']) 
		{
 			die('You did not fill in a required field.');
	 	}
 		$query = mysql_query("SELECT * FROM users WHERE name_username = '".$_POST['userName']."'") or die('error'); //
	 	//Gives error if user dosen't exist
		$check = mysqli_num_rows($query);
		if ($check == 0) 
		{
 			die('That user does not exist in our database. <a href=registration.php>Click Here to Register</a>');
		}
		 while($info = mysqli_fetch_array( $query )) 	
		{
 			$_POST['passwordLogin'] = stripslashes($_POST['passwordLogin']);
 			$info['password'] = stripslashes($info['password']);
 			//gives error if the password is wrong
 			if ($_POST['passwordLogin'] != $info['password']) 
			{
 			die('Incorrect password, please try again.');
 			}
			else 
			{ 	
			header('profile.php');
			}
		}
	}
}

 

This line:

$query = mysql_query("SELECT * FROM users WHERE name_username = '".$_POST['userName']."'") or die('error'); //

 

gives the die error.

This line:


$query = mysqli_query("SELECT * FROM users WHERE name_username = '".$_POST['userName']."'") or die('error'); 

 

 

Gives the die error and this error:


Warning: mysqli_query() expects at least 2 parameters, 1 given in /Users/****/Sites/php-class/module3/functions.php on line 110

When I add the database to the mysqli_query:

	$query = mysqli_query($dbc,"SELECT * FROM users WHERE name_username = '".$_POST['userName']."'") or die('error');

 

it works!!! Thank you so much!

Edited by wildbuddha
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.