How to get the following to work with left join tables


#1

I am trying to follow this tutorial on how to do a left join and I have the following codes... I can get it to work only using these codes..I am trying to understand what he is doing at 7:19 but I guess that he is using pdo and not mysqli? I am trying to learn about mysqli... thanks!

include_once 'includes/dbh.php';

$sql = "SELECT * FROM users";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if($resultCheck > 0) {
	while($row = mysqli_fetch_assoc($result)) {
		echo $row['user_first'];

		$permission = "SELECT * FROM permissions";

		$results = mysqli_query($conn, $permission);
		$resultsCheck = mysqli_num_rows($results);
		if($resultsCheck >0) {
			while($rows = mysqli_fetch_assoc($results)) {
				
				echo $rows['name'];
			}
		}
		
	} 
	}else {
		echo 'No result';
}

However, I can't get it to work when I tried to add some sort of id....

These are my database tables from both users and permissions table



#2

I don't think so. It looks like he is using object notation instead of array notation.

Even if you never write OOP code it is a good idea to learn the syntax so that you can use others code where you have no choice but to use object notation. Anyway, don't let this distract you from following the tutorial, it's focus is on JOINs. So even though the syntax will look a bit different you should still be able to learn from it.


#3

thanks for the kind assistance!


#4

Hey guys!

I can get my left join to work by doing these codes but the problem now is that how would I grab the username from the login? At the moment, it will only work if I match to a particular name in my database..

<?php
include_once 'includes/dbh.php';



$sql = "SELECT users.user_first, user_access.access_type as accesslevel FROM users
         LEFT JOIN user_access ON users.user_access = user_access.id;

";



$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if($resultCheck > 0) {
	while($row = mysqli_fetch_assoc($result)) {
		if ($row['accesslevel'] == 'primer level' && $row['user_first'] == 'Mervin') {
			echo 'Welcome to primer level contents';
		} else {
			echo 'You do not have permissions';
			
					}
		
		
	} 
	}else {
		echo 'No result';
}

I am only trying to grant access to the user if they are on primer level only...... based on their user id....


#5

I think i got it.... I just got rid of $row['user_first'];


#7

I can get it to echo out the correct information from the following codes:

<?php
include_once 'includes/dbh.php';



$sql = "SELECT users.user_first, user_access.access_type as accesslevel FROM users
         LEFT JOIN user_access ON users.user_access = user_access.id

";



$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if($resultCheck > 0) {
	  while($row = mysqli_fetch_assoc($result)) {
         echo $row ['user_first'];
         echo $row['accesslevel']."<br>";
		}
		
	
	}else {
		echo 'No result';
}

The following codes does work as well:

<?php
include_once 'includes/dbh.php';



$sql = "SELECT users.user_first, user_access.access_type as accesslevel FROM users
         LEFT JOIN user_access ON users.user_access = user_access.id

";



$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if($resultCheck > 0) {
	  while($row = mysqli_fetch_assoc($result)) {
         if ($row['accesslevel'] != 'primer level') {
			echo $row['user_first'];
			exit();
		} else {
			echo ' You must do this....';
			
		}
		
	
	}
}

It won't give my other user access even though that she is set to primer level

This is my database table for user_access

But if I do this, then it won't work:

include_once 'includes/dbh.php';



$sql = "SELECT users.user_first, user_access.access_type as accesslevel FROM users
         LEFT JOIN user_access ON users.user_access = user_access.id

";



$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if($resultCheck > 0) {
	  while($row = mysqli_fetch_assoc($result)) {
         if ($row['accesslevel'] != 'primer level') {
			header("Location: index.php?signup=accessdenied");
			exit();
		} else {
			echo ' Welcome to the content';
			
		}
		
	
	}
}

I granted my second user Lucy888 permission but she can't access the page


#8

I can't see anything in that code that is checking permission for a specific user, it's just getting the information for all users and then checking each one. That last piece of code retrieves both user rows, then runs through each of them in turn, but in no specific order because you don't have an ORDER BY clause.

In this case it appears that it gets the two users in the order they are displayed in the image, sees that the first one has a value of '2' in the user_access column, this doesn't equate to primer level in your other table, so it does a redirect and never gets to the second record.


#9

I thought that if I do this.. it will see if ($row['accesslevel'] == 'primer level') and that should work? Only users with the primer level can access this page?


#10

I think i understand... i should have selected users.user_uid in my sql and used that to check with the id


#11

Well, you just need to specify which user you want to check the permission levels for. At the moment, you're checking all of them, and it just happens that the first one it recovers isn't a "primer level" user, so it redirects.

If it's part of a login system, then you should only retrieve the information you need for the user that is trying to log in, with a WHERE clause in your query.


#12

I am using my phone now so can't copy and paste but initially i did add where user_uid='$uid'; but i got something about undefined $uid. I am doing a login system so i am not sure how to pass the information that the user has used while logging in to this page called primer.php.

Should I have used inner join instead? Because an user always need some kind of permission?


#13

It's nothing to do with the kind of join you use. You said earlier that the first bit of code to just display the user details works OK, so the query must be alright. The issue is that you're just looking at permissions for all users, not just the one who is trying to log in.

Where were you getting the value of $uid from?

Is that not part of the form? So it would pass the form variables through to this page in either $_GET or $_POST arrays, depending on how your form is configured?


#14

In your database table user_access you don't have id of user stored in that table so i don't see a reason to join tables.

You can simple check like this
for multiple users

// for multiple users if you don't have ID
$query = mysqli_query($conn, "SELECT * FROM users");
$resultCheck = mysqli_num_rows($query);

// if there is row returned
if ($resultCheck > 0) {

    // loop to get user access
    while ($row = mysqli_fetch_assoc($query)) {
        if ($row['user_access'] != 2 && $row['user_activate'] != 1) {
            header("Location: index.php?signup=accessdenied");
            exit();
        } else {
            echo ' Welcome to the content';    
        }
    } // while
}

and for a single user, you must provide user_id

// for single user if you got ID
$query = mysqli_query($conn, "SELECT * FROM users WHERE id = '".$id."'");
$resultCheck = mysqli_num_rows($query);

// if there is row returned
if ($resultCheck > 0) {

    // loop to get user access
    while ($row = mysqli_fetch_assoc($query)) {
        if ($row['user_access'] != 2 && $row['user_activate'] != 1) {
            header("Location: index.php?signup=accessdenied");
            exit();
        } else {
            echo ' Welcome to the content';    
        }
    } // while

}

#15

Maybe I misunderstood what the joined table is trying to do... perhaps i only need the joined table for my own information but i guess i should just select user_uid and match it with user_access=1..


#16

Yes, the join is just to allow you to display a name against each of the permission levels. Handy if you insist on having a value of "2" meaning "level 1", for example, but not required for actually performing the check.


#17

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.