Your function might be rewritten for MySQLi like so:
Ok, thanks, your code worked for me - but I want to understand it. I think the reason it wasn't working was b/c I am using mysqli in my database connection.....and in the tutorial on Youtube, its wasn't...is that why all this wasn't working from the get go? Below are my files that all played a role in this working.
Login.php
<?php
// this file processes the username and password, and calls different functions, is the user active.
// to handle error handling (i.e - wrong password) we create an array called $errors in init.php
include 'core/init.php';
// include 'core/database/connect.php';
ini_set('error_reporting',-1);
ini_set('display_errors','On');
# 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) === 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?";
}
}
?>
Connect.php
<?php //// This is what the code looks like after Database Connection
$link = mysqli_connect('localhost','root','', 'lr');
if (!$link) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
// echo 'Success... ' . mysqli_get_host_info($link) . "\n";
mysqli_close($link); // ended here
?>
Users.php
function check_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 1 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;
Question #1:
function check_username( $username,mysqli $DB ){
What are we saying above in english? We are going create a function called "check_username".....we want the $username variable to be used by this fuction?, right ....and ???? what does having mysqli and a variable called $DB there for? Can someone explain that to me when we have a mysql and a variable beside eachother? What is getting stored in $DB??
Question #2:
# sanitize $username (to avoid SQL injection attacks/errors)
$username = $DB->real_escape_string( $username );
This question is partly because I don't know what's happening on with $DB and what its storing....but is the sequence like this? Take the data stored in $username and pass it through $DB, then pass that data through the real_escape_function, and pipe it back out into the $username variable? ....almost like an assembly line of sorts?
Thanks for all the help,
Adrian