Jump to content

Collect All Names And See If Username Is = To List Of Names


Recommended Posts

I have a piece of code that I'm not sure why it isn't right. I have a list of names in a SQL table and I want to see if the current username matches any of the usernames in the list and if not redirect it to another page. Here's what I have:

 

global $user;
$username = $user->name;
$query = mysql_query("SELECT submittedusername FROM org");
$rows = mysql_fetch_array($query);
If ($rows != $username) {
$yquery = drupal_get_destination();
drupal_goto('/node/registerfirst',$yquery);
}

 

Where an i going wrong?

How would I do that? Not sure what you are saying.

 

Use just SQL to call it and see if it's in there?

 

So:

 

global $user;
$username = $user->name;
$query = mysql_query("SELECT submittedusername FROM org WHERE submittedusername = '$username'");

 

but this how would I do "if username isn't in the database redirect to different page"

This is the logic that would probably be good for you, IMHO.

 

$query = "SELECT COUNT(1) FROM table WHERE field = '$value'";
$result = mysql_query( $query );
$array = mysql_fetch_row( $result );
if( $array[0] === 1 ) {
      // name in in DB only once, as it should be.
} else {
      if( $array[0] === 0 ) {
             // name was not found
      }
      if( $array[0] > 1 ) {
             // name appears more than once, indicating data problems
      }
}

 

Do note that you need to validate the input, if the username comes from the user. This is typically done with the ctype_* () functions or Regular Expressions.

You'll also need to escape the output, to prevent SQL injections. This is done with mysqli::real_escape_string ().

 

PS: You should move away from the old and outdated mysql library, and use the new and up-to-date mysqli (for "improved") library or PDO. Both of them are explained in detail in the PHP manual.

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.