TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Problem with sql result echo (http://www.talkphp.com/general/5391-problem-sql-result-echo.html)

SaintIsaiah 04-04-2010 05:28 AM

Problem with sql result echo
 
I'm modifying a CMS to create an if/else statement to prevent a journalist who is not an editor from seeing any articles except the ones they made. However, it seems that the code I made to get their usergroup ID is not getting the result to use for the if/else statement. I'm trying just to echo the user's group id to see if it just works first so I can continue finishing the code. Here's what I got.

PHP Code:

$usernamesql $userinfo['username']; //$userinfo['username'] is defined in another part of the page and already works

  
$getugroupid mysql_query("SELECT * FROM pam_users WHERE username = '.$usernamesql.'"); //Trying to select all of the user's info in the DB

$ugroupid mysql_fetch_array($getugroupid); //Trying to create an array to echo values from

echo '<center><h1>'$ugroupid['usergroupid'] .'</h1></center>';//'usergroupid' is the name of the column in the db containing that user's group id number 

However, it never shows the user's group id. Any idea what I'm doing wrong?

delayedinsanity 04-04-2010 02:59 PM

Without seeing the database it's hard to tell. Have you tried the following?

php Code:
// Your code, then
echo '<pre>' . print_r( $ugroupid, TRUE ) . '</pre>';

// or simply

$result = mysql_query( sprintf( "SELECT usergroupid FROM pam_users WHERE username = '%s' LIMIT 1", $userinfo['username'] ) );
$ugroupid = mysql_result( $result );

Don't forget to double check your $userinfo array and make sure username is properly set as well.

SaintIsaiah 04-04-2010 08:03 PM

Well I just got it working. Here's what I did:

PHP Code:

$usernamesql $userinfo['username'];

$getugroup $DB->query("SELECT * FROM {users} WHERE username = '".$usernamesql."'");

$ugroupids $DB->fetch_array($getugroup);

$ugroupid $ugroupids['usergroupid'];

echo 
$ugroupid

The $DB variable was already defined as well. I guess it was just a syntax error.

What I did was pretty much used an if/else statement to show what info a user is allowed to see based on what usergroup they're in.

PHP Code:

if (($ugroupid != '1') && ($ugroupid != '6') && ($ugroupid != '7') && ($ugroupid != '8'))  {

//Restricted Access

} else {

//Full Access



It works, but I was wondering how to make an if statement that will check all of those group id numbers at once, like an if array statement. That possible?

delayedinsanity 04-04-2010 08:16 PM

php Code:
if ( in_array( $ugroupid, array( 1, 6, 7, 8 ) ) ) {
//do stuff
}

SaintIsaiah 04-05-2010 04:59 PM

Quote:

Originally Posted by delayedinsanity (Post 30387)
php Code:
if ( in_array( $ugroupid, array( 1, 6, 7, 8 ) ) ) {
//do stuff
}

Sweet, that works perfectly!


All times are GMT. The time now is 03:33 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0