Niixie Posted January 26, 2011 Share Posted January 26, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/225771-resource-id-51/ Share on other sites More sharing options...
Maq Posted January 26, 2011 Share Posted January 26, 2011 That's what it's supposed to output. If you want extract the 'adminlevel' value you must you something like mysql_fetch_array. There are plenty of examples in the manual. Quote Link to comment https://forums.phpfreaks.com/topic/225771-resource-id-51/#findComment-1165588 Share on other sites More sharing options...
ChemicalBliss Posted January 26, 2011 Share Posted January 26, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/225771-resource-id-51/#findComment-1165589 Share on other sites More sharing options...
Niixie Posted January 26, 2011 Author Share Posted January 26, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/225771-resource-id-51/#findComment-1165592 Share on other sites More sharing options...
Pikachu2000 Posted January 26, 2011 Share Posted January 26, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/225771-resource-id-51/#findComment-1165596 Share on other sites More sharing options...
ChemicalBliss Posted January 26, 2011 Share Posted January 26, 2011 Check my post nixie and tell us what it says, one problem at a time... Quote Link to comment https://forums.phpfreaks.com/topic/225771-resource-id-51/#findComment-1165599 Share on other sites More sharing options...
Maq Posted January 26, 2011 Share Posted January 26, 2011 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'; } Quote Link to comment https://forums.phpfreaks.com/topic/225771-resource-id-51/#findComment-1165600 Share on other sites More sharing options...
Niixie Posted January 26, 2011 Author Share Posted January 26, 2011 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.) Quote Link to comment https://forums.phpfreaks.com/topic/225771-resource-id-51/#findComment-1165604 Share on other sites More sharing options...
ChemicalBliss Posted January 26, 2011 Share Posted January 26, 2011 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>'; } Quote Link to comment https://forums.phpfreaks.com/topic/225771-resource-id-51/#findComment-1165605 Share on other sites More sharing options...
Niixie Posted January 26, 2011 Author Share Posted January 26, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/225771-resource-id-51/#findComment-1165607 Share on other sites More sharing options...
ChemicalBliss Posted January 26, 2011 Share Posted January 26, 2011 Then you want to change your query: "SELECT `name` FROM accounts WHERE name = '".$_SESSION['auth_username']."' AND adminlevel='1'" Quote Link to comment https://forums.phpfreaks.com/topic/225771-resource-id-51/#findComment-1165610 Share on other sites More sharing options...
Niixie Posted January 26, 2011 Author Share Posted January 26, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/225771-resource-id-51/#findComment-1165611 Share on other sites More sharing options...
Maq Posted January 26, 2011 Share Posted January 26, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/225771-resource-id-51/#findComment-1165613 Share on other sites More sharing options...
Niixie Posted January 26, 2011 Author Share Posted January 26, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/225771-resource-id-51/#findComment-1165614 Share on other sites More sharing options...
ChemicalBliss Posted January 26, 2011 Share Posted January 26, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/225771-resource-id-51/#findComment-1165615 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.