Jump to content

PHP/MySQL - Show page based on user level?


silver9990

Recommended Posts

Here's my problem, I've been trying to get this to work for 8 hours:

 


//connection info and query info

if ($row["group"] == "1") {

show

} else {

don't show

}

 

I can't remember the exact codes I've tried but none of them have worked, I always end up with this:

 

Warning: mysql_num_rows(): supplied argument is not a valid mysql result resource in file.php on line x

 

Any help would be appreciated.

Link to comment
https://forums.phpfreaks.com/topic/74999-phpmysql-show-page-based-on-user-level/
Share on other sites

Use this:

 

$q = mysql_query("SELECT * FROM `table` WHERE group = '1'");
$r = mysql_fetch_array($q);
if($r['group'] == COOKIE/SESSION) {
   echo "Has access to the page";
} else {
   echo "Sorry, you do not have access to this page";
}

 

Change the COOKIE/SESSION to whatever you use to store the users level who is browsing the page.

Thanks for the replies but both give me errors.

 

I've added the correct info for both and this is what shows up:

 

"localhost","dbuser","pass":

Parse error: syntax error, unexpected ',' in test.php on line 3

 

$r = mysql_fetch_array($q);:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in test2.php on line 9

Here:

 

sql.inc.php:

$db_user = ""; // Username
$db_pass = ""; // Password
$db_database = ""; // Database Name
$db_host = ""; // Server Hostname

// DO NOT EDIT ANYTHING AFTER THIS LINE!
$db_connect = @mysql_connect ($db_host, $db_user, $db_pass);
$db_select = @mysql_select_db ($db_database);

 

page.php:

include("sql.inc.php");

$q = mysql_query("SELECT * FROM `table` WHERE group = '1'") or die (mysql_error());
$r = mysql_fetch_array($q);

if($r['group'] == COOKIE/SESSION) {
// has access
} else {
// Doesnt have access
}

 

Make sure you change the db info and the sql info to your server info.

why don't you do it like this?

 

<?php

// I'm presuming you have a session login, so do this...

$sql = mysql_query("SELECT * FROM `member_table` WHERE `username_column` = '". $_SESSION['session'] ."'");

while($row = mysql_fetch_assoc($sql)){

if($row['group'] != 1){

// Whatever error screen you want to show if the user isn't authorized
exit();

}else{

// The page

}
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.