Jump to content

Resource id #51


Niixie

Recommended Posts

Hey PHPFreaks.

 

I made a php code, thats only needed to be showed for admin accounts only.

 

I tryed to echo the

mysql_num_rows($result);

and it gave me this:

Resource id #51

 

Heres a piece of my code where the problem is:

$result = mysql_query("SELECT adminlevel FROM accounts WHERE name = '".$_SESSION['auth_username']."'") or die(mysql_error());
echo $result;
echo mysql_num_rows($result);
if(mysql_num_rows($result) == 1) {
echo '<br /><br /><a href="home.php?admin">Admin Area</a>';
}

 

Hope you can help

Link to comment
Share on other sites

When you "echo" a literal "integer" (a number that php knows is not a string), then you must turn it into a string to echo it properly.

 

eg:

$result = mysql_query("SELECT adminlevel FROM accounts WHERE name = '".$_SESSION['auth_username']."'") or die(mysql_error());
echo $result;
echo "Num rows: ".mysql_num_rows($result);
if(mysql_num_rows($result) == 1) {
echo '<br /><br /><a href="home.php?admin">Admin Area</a>';
}

 

Also i would change your If function:

if(mysql_num_rows($result) !== false) {
echo '<br /><br /><a href="home.php?admin">Admin Area</a>';
}

 

Hope this helps

Link to comment
Share on other sites

Now i tryed some stuff, these are some of it.

 

if(mysql_fetch_array($result, MYSQL_NUM) !== false) {
echo '<br /><br /><a href="home.php?admin">Admin Area</a>';
}

// And

if(mysql_fetch_array($result, MYSQL_NUM) == 1) {
echo '<br /><br /><a href="home.php?admin">Admin Area</a>';
}

// and

if(mysql_fetch_array($result) !== false) {
echo '<br /><br /><a href="home.php?admin">Admin Area</a>';
}

// and

if(mysql_fetch_array($result) == 1) {
echo '<br /><br /><a href="home.php?admin">Admin Area</a>';
}

 

Now nothing shows?

Link to comment
Share on other sites

You should be able to echo mysql_num_rows($result) just fine. The problem in the code above seems to be the attempt to echo a result resource directly:

 

$result = mysql_query("SELECT adminlevel FROM accounts WHERE name = '".$_SESSION['auth_username']."'") or die(mysql_error());
echo $result; // <--- variable this holds the result resource

Link to comment
Share on other sites

Read the comments.

$result = mysql_query("SELECT adminlevel FROM accounts WHERE name = '".$_SESSION['auth_username']."'") or die(mysql_error());
echo $result; //This is the output for Resource #51
echo mysql_num_rows($result); //This should output the number of rows.  Do you see this?
if(mysql_num_rows($result) == 1) { //did you want to check how many rows are returned or what the 'adminlevel' is?
echo '

Admin Area';
}

Link to comment
Share on other sites

Check my post nixie and tell us what it says, one problem at a time...

 

The echo's

echo $result;
echo "Num rows: ".mysql_num_rows($result);

is just for debugging, no problem there.

 

Read the comments.

$result = mysql_query("SELECT adminlevel FROM accounts WHERE name = '".$_SESSION['auth_username']."'") or die(mysql_error());
echo $result; //This is the output for Resource #51
echo mysql_num_rows($result); //This should output the number of rows.  Do you see this?
if(mysql_num_rows($result) == 1) { //did you want to check how many rows are returned or what the 'adminlevel' is?
echo '<br /><br /><a href="home.php?admin">Admin Area</a>';
}

 

I didnt see

echo mysql_num_rows($result); //This should output the number of rows.  Do you see this?

before i removed it. So the echos are deleted now.

 

And i want the

if(mysql_num_rows($result) == 1)

too see if the person thats logged in is

=> 1

admin level

(I know its wrong, but i'd like it to not show in the menu in the first place.)

Link to comment
Share on other sites

lol you didn't really produce any answers but just a shot in the dark:

 

$result = mysql_query("SELECT adminlevel FROM accounts WHERE name = '".$_SESSION['auth_username']."'") or die(mysql_error());
echo $result;
echo "Num rows: ".mysql_num_rows($result);
if(mysql_num_rows($result) <= 0) {
echo 'RESTRICTED';
}else{
echo '<br /><br /><a href="home.php?admin">Admin Area</a>';
}

Link to comment
Share on other sites

Didnt work.

 

Let me tell you then, it needs to go in my database. Pick the table slot 'adminlevel' in the table 'accounts' where the name is 'ex. Niixie'.

Then, if the adminlevel is equal or above 1 then let him/her see

echo '<br /><br /><a href="home.php?admin">Admin Area</a>';

else dont.

Link to comment
Share on other sites

Then you want to change your query:

 

"SELECT `name` FROM accounts WHERE name = '".$_SESSION['auth_username']."' AND adminlevel='1'"

 

Then it only selects the player if his adminlevel is 1, what if i want it 1 or above?

 

You need something a bit more dynamic.  Try something like this:

$result = mysql_query("SELECT adminlevel FROM accounts WHERE name = '".$_SESSION['auth_username']."'") or die(mysql_error());
$row = mysql_fetch_array($result);
if($row['adminlevel'] >= 0) 
{
   echo '

Admin Area';
}
else
{
      echo 'do something';
}

 

This way you can check against the 'adminlevel' which will allow you to perform whatever action on any level.

 

Link to comment
Share on other sites

Then you want to change your query:

 

"SELECT `name` FROM accounts WHERE name = '".$_SESSION['auth_username']."' AND adminlevel='1'"

 

Then it only selects the player if his adminlevel is 1, what if i want it 1 or above?

 

You need something a bit more dynamic.  Try something like this:

$result = mysql_query("SELECT adminlevel FROM accounts WHERE name = '".$_SESSION['auth_username']."'") or die(mysql_error());
$row = mysql_fetch_array($result);
if($row['adminlevel'] >= 0) 
{
   echo '<br /><br /><a href="home.php?admin">Admin Area</a>';
}
else
{
      echo 'do something';
}

 

 

Wow, that worked. Thank you very much :)

Link to comment
Share on other sites

Just for reference, MySQL also supports the >= (greater than or equal to) operator.

 

eg,

$result = mysql_query("SELECT `name` FROM accounts WHERE name = '".$_SESSION['auth_username']."' AND adminlevel>='1'") or die(mysql_error());
echo $result;
echo "Num rows: ".mysql_num_rows($result);
if(mysql_num_rows($result) <= 0) {
echo 'RESTRICTED';
}else{
echo '<br /><br /><a href="home.php?admin">Admin Area</a>';
}

 

But Maq's method is much more future-proof as you can check if the user has an adminlevel above a certain number several times without using mysql again, whereas above, if you had to check if the user had an access level above a different (higher) number then you would need another query.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.