wildbuddha Posted April 1, 2013 Share Posted April 1, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/276369-login-function-cant-find-db-but-other-functions-can/ Share on other sites More sharing options...
Solution mac_gyver Posted April 1, 2013 Solution Share Posted April 1, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/276369-login-function-cant-find-db-but-other-functions-can/#findComment-1422194 Share on other sites More sharing options...
wildbuddha Posted April 1, 2013 Author Share Posted April 1, 2013 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! Quote Link to comment https://forums.phpfreaks.com/topic/276369-login-function-cant-find-db-but-other-functions-can/#findComment-1422195 Share on other sites More sharing options...
wildbuddha Posted April 1, 2013 Author Share Posted April 1, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/276369-login-function-cant-find-db-but-other-functions-can/#findComment-1422196 Share on other sites More sharing options...
wildbuddha Posted April 1, 2013 Author Share Posted April 1, 2013 (edited) 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 April 1, 2013 by wildbuddha Quote Link to comment https://forums.phpfreaks.com/topic/276369-login-function-cant-find-db-but-other-functions-can/#findComment-1422197 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.