简体   繁体   中英

Php session, redirect based on user level issue

i have this code which is not seeming to work, what i want to do is make a query to understand users' level, and then based on that level i need to print different content, for the moment it prints the default one, and i get this error message:

mysql_result() expects parameter 1 to be resource, boolean given on this line:

$rank = mysql_result($rank1, 0, 'rank');

the code is like this:

session_start();
if(!$_SESSION['username']){

header("location:login.php"); // Redirect to login.php page
}
else //Continue to current page
header( 'Content-Type: text/html; charset=utf-8' );
$rank1 = mysql_query("SELET access FROM tbl_galleries WHERE column='username" . mysql_real_escape_string($_SESSION['username']) . "'");
$rank = mysql_result($rank1, 0, 'rank');

switch ($rank)
{
case 3:
  echo "<div><a href='#'>Order DIAMOND Membership</a></div>";
  break;

case 2:
  echo "<div><a href='#'>Order PLATINUM Membership</a></div>
    <div><a href='#'>Order DIAMOND Membership</a></div>";
  break;

case 1:
  echo "<div><a href='#'>Order ELITE Membership</a></div>
    <div><a href='#'>Order PLATINUM Membership</a></div>
    <div><a href='#'>Order DIAMOND Membership</a></div>";
  break;

default:
  echo "<div><a href='#'>Order PRO Membership</a></div>
    <div><a href='#'>Order ELITE Membership</a></div>
    <div><a href='#'>Order PLATINUM Membership</a></div>
    <div><a href='#'>Order DIAMOND Membership</a></div>";
}

你有语法错误, SELET必须是SELECT

mysql_query("SELECT..

You are mismatching your quotes in the query and using improper syntax:

WHERE column='username" 

Try using:

mysql_query("SELECT access FROM tbl_galleries WHERE username ='" . mysql_real_escape_string($_SESSION['username']) . "'");

Okay, tricky syntax:

$rank1 = mysql_query("SELECT access FROM tbl_galleries WHERE username" . mysql_real_escape_string($_SESSION['username']) . "'");
$rank = mysql_result($rank1, 0, 'rank');

You are using optional params to get a particular column back in the mysql_result but it does not match the column name you are selecting in your query statement;

$rank1 = mysql_query("SELECT access FROM tbl_galleries WHERE username" . mysql_real_escape_string($_SESSION['username']) . "'");
$rank = mysql_result($rank1, 0, 'access');

Do you have a Mysql connection configured. You should have something like that: $link = mysql_connect('localhost', 'mysql_user', 'mysql_password');

Why not try this:

$username = mysql_real_escape_string($_SESSION['username']);
$rank1 = mysql_query("SELECT access FROM tbl_galleries WHERE username = '$username'");

In your mysql query u are retrieving the only single column named access from your table and in your further code in mysql_result() function you are passing the third parameter field name as rank but in your query returned result the column or field name rank is not coming . Like this

SELECT access FROM tbl_galleries WHERE username = '$username'
$rank = mysql_result($rank1, 0, 'rank');

If you want to get result of rank field then either retrieve the rank field with the field access or retrieve all columns of your table or pass the third parameter in mysql_result function as 'access'

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

This means you have some error in your query. Try to finalize the query in phpMyAdmin or some other such tool. Your query should go something like this

$userName = mysql_real_escape_string($_SESSION['username']);
$sql = "SELECT access 
        FROM tbl_galleries 
        WHERE username = $userName";
$result = mysql_query($sql);
$rank = mysql_result($result, 0, 'access');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM