Jump to content


Photo

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


Best Answer mac_gyver, 31 March 2013 - 09:09 PM

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.

Go to the full post


  • Please log in to reply
4 replies to this topic

#1 wildbuddha

wildbuddha

    Member

  • Members
  • PipPip
  • 10 posts
  • LocationNew York, NY

Posted 31 March 2013 - 08:35 PM

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?

Attached Files



#2 mac_gyver

mac_gyver

    Advanced Member

  • Administrators
  • 2,618 posts

Posted 31 March 2013 - 09:09 PM   Best Answer

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.


multi-purpose programming fool. well written source-code should be self-documenting. well written code should be self-troubleshooting. 


#3 wildbuddha

wildbuddha

    Member

  • Members
  • PipPip
  • 10 posts
  • LocationNew York, NY

Posted 31 March 2013 - 09:11 PM

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!



#4 wildbuddha

wildbuddha

    Member

  • Members
  • PipPip
  • 10 posts
  • LocationNew York, NY

Posted 31 March 2013 - 09:20 PM

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



#5 wildbuddha

wildbuddha

    Member

  • Members
  • PipPip
  • 10 posts
  • LocationNew York, NY

Posted 31 March 2013 - 09:24 PM

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, 31 March 2013 - 09:28 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com