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
PHP Code:
function user_ID_from_username( $username,mysqli $DB ){
# 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 -
PHP Code:
function user_ID_from_username( $username,mysqli $DB ){
This is line 49 -
PHP Code:
$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
PHP Code:
# 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?
The function user_ID_from_username requires two parameters. You have only provided one in this function call:
PHP Code:
$user_id = user_id_from_username($username);
Also, that function as you have defined it doesn't make any sense. You are fetching a query result from a database and then returning the number of rows returned -- cast as a boolean? A boolean is TRUE or FALSE. Also, the number of rows returned is not the id of a particular user but rather the number of matching records.
ok, l stepped back to make sure I understand this....and you're right...I copied that query boolean thing from some other code I have so it doesn't make sense...
here is the code from the tutorial - what is this query saying in english? what does the 0 means at the end of this query? return row 0?
PHP Code:
function user_id_from_username ($username) {
$username = sanitize($username);
return mysql_result(mysql_query(SELECT user_id FROM `users` WHERE `username` = '$username'"), 0, 'user_id');
here is what I did to troubleshoot:
1. since the above query won't work with mysqli connection - I ran a query in phpmyadmin which it returned - 1 (user_id = 1)
/* Select queries return a resultset */
if ($result = $mysqli->query("SELECT `user_id` FROM `users` where `username`='$username'")) {
printf("Select returned %d rows.\n", $result->num_rows);
echo $result;
/* free result set */
$result->close();
}
- it displayed "Select returned 1 rows" in my browser. I copied this from the php man page for mysql query so it is working....so now I guess my question (besides the ones above) is how to return the actual value (user_id = "1") and not the number of rows affected...I realize that the
PHP Code:
$result->num_rows);
is doing this...what is $result->num_rows); - a method for mysqli??
One very very important thing you need to pay attention to is that mysql and mysqli are not the same thing. The first one (no "i" in it) is the old mysql connection extension. The new one (with the "i" at the end) is the improved one and that's what the i stands for. You should not use the old one because it has been deprecated and will be going away. Do not mix mysql_* and mysql_* functions -- they are totally different and do not work together. Use one or the other. [use the new one!]
Looking at your tutorial code:
PHP Code:
function user_id_from_username ($username) { $username = sanitize($username); return mysql_result(mysql_query(SELECT user_id FROM `users` WHERE `username` = '$username'"), 0, 'user_id');
Two things occur to me:
1) that's not the entire function
2) It's bad form to feed the result of mysql_query to mysql_result without checking first to see if mysql_query was successful or not.
3) The last line of that snip is missing a quote right before SELECT and would therefore cause a syntax error.
That said, your tutorial sounds pretty suspect to me. If you want to understand what parameters you are supplying to mysql_result, check the PHP documentation for mysql_result. The PHP documentation is pretty darn good as software language documentation goes and learning to use it will really benefit you a lot in the long run. And, again, you should be using the mysqli functions and not the old mysql ones. The extra parameters, the zero and "user_id" are being supplied to mysql_result, not mysql_query.
Always the check of a query to make sure it worked before you try to do anything with it.
This query:
PHP Code:
$result = mysql_query("SELECT user_id FROM `users` WHERE `username` = '$username'")
Says "fetch me all the records from the table "users" where the username column contains the value in my PHP variable $username. mysql_query will return a mysql resource object which you can extract records and values from with other functions like mysql_fetch_assoc. Again, you should be using the mysqli equivalents and not the old ones.
Functions like mysqli_fetch_assoc take the result of a mysqli_query function call and extract individual records from it. Use the examples in the manual as your starting point. The basic idea is that mysqli_query runs a query that might consist of a million rows and returns a data resource pointer. mysqli_fetch_assoc makes use of that data resource pointer and extracts records one at a time as an associative array.
Your troubleshooting step is a good one--run the query in phpMyAdmin to see if it's legit. Keep in mind that errors in your PHP may result in syntax mistakes in your sql statements. In your function copied from the php documentation, you are almost there. Instead of echoing the output of $result->num_rows, consider something like this:
PHP Code:
// fetch an associative array for each record returned by the result while ($row = $result->fetch_assoc()) { // a record was returned! Let's echo it out to see what's in it: print_r($row); }
Under thread tools at the top when you are viewing the thread.
Sadly, nobody codes for anyone on this forum. People taste your dishes and tell you what is missing, but they don't cook for you. ~anoopmail I'd rather be a comma, then a full stop. User Authentication in PHP with MySQLi - Don't forget to mark threads resolved - MySQL(i) warning
Bookmarks