Hi guys,
I'm still going through these tutorials...going through this one below:
http://www.youtube.com/watch?v=JUk2e8oqOvI&list=ECE134D877783367C7&index=8
I am using mysqli for my database connection (whereas the guy in the video is not, and he is also what seems to me using long complicated queries?...I figure they can be wrote more simpler...), when I am creating the functions to work with mysqli, I am getting an error when I login:
Catchable fatal error: Argument 2 passed to user_ID_from_username() must be an instance of mysqli, string given, called in C:\wamp\www\LoginRegister\core\functions\users.php on line 49 and defined in C:\wamp\www\LoginRegister\core\functions\users.php on line 33
Below is my code:
users.php
function user_ID_from_username( $username,mysqli $DB ){
# sanitize $username (to avoid SQL injection attacks/errors)
$username = $DB->real_escape_string( $username );
# this is the SQL query we'll use.
$sql = "SELECT `user_id` FROM `users` where `username`='$username'";
# execute the query.
$result = $DB->query( $sql );
# check if there were any rows in the result (if there were no rows, the username does not exist).
return (bool)$result->num_rows;
}
function login( $username, $password,mysqli $DB ) {
$user_id = user_id_from_username($username);
#sanitize username and hash password
$username = sanitize($username);
$password = md5($password);
# this is the SQL query we'll use.
$sql = "SELECT 1 FROM `users` WHERE `username`='$username' AND `password`='$password'";
# execute the query.
$result = $DB->query( $sql );
# check if there were any rows in the result (if there were no rows, the username and password do not match).
return (bool)$result->num_rows;
}
This is line 33 -
function user_ID_from_username( $username,mysqli $DB ){
This is line 49 -
$user_id = user_id_from_username($username);
If I comment out line 49 - I can login successfully and it also let's me know if I typed in the worng password....so the "login" function I created is working...its just this "user_ID_from_username" function that is giving me the error...
Index.php
# database connection
$DB = new mysqli( 'localhost','root','', 'lr' );
# username to check
//$username = 'adrian';
//if( check_username( $username,$DB ) ){
# check_username() returned TRUE.
// print "The username '$username' already exists.";
//}else{
// # check_username() returned FALSE.
// print "The username '$username' is available.";
//}
if (empty($_POST) === false) { // === checks for type
$username = $_POST['username']; //define the $username variable and point it back to the other login.php file where it says - <input type="text" name="username">
$password = $_POST['password']; //define the $password variable and point it back to the other login.php file where it says - <input type="password" value="password">
if(empty($username) === true || empty($password) === true) {
$errors[] = 'You need to enter a username and password'; //if the username or password is empty, display the code
} else if(user_exists($username,$DB ) === false) { //WORKFLOW: we are checking if the user exists, which is calling the user_exists fucntion user_exists in users.php, which then passes in the username, sanitizing it by calling the sanitize function in the general.php, and then we run our query.
$errors[] = 'We can\'t find the username. Have your registered?';
} else if(user_active($username,$DB ) === false) {
$errors[] = 'You haven\'t activated your account!;';
} else {
$login = login($username, $password, $DB);
if ($login === false) {
$errors[] = 'That username/password combination is incorrect';
} else {
echo 'ok';
// set the user session
// redirect user to home
}
}
print_r($errors);
}
I have an idea what this error is referring to - its saying it wants a proper arguement in the function - I tried putting in the $password variable in the brackets on line 49, but that didn't work....does my sql query in this funciton make sense?
Thanks in advance,