Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8280351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:38:50+00:00 2026-06-08T09:38:50+00:00

I am trying to write a simple function that checks if a username exists

  • 0

I am trying to write a simple function that checks if a username exists in the db and if so to call another function to generate a new username. My code seems to fall over though:

Username Function:-

$user1=create_username($fname, $company);

function create_username($surname, $company){

//$name_method=str_replace(" ", "", $surname);
$name_method=$surname.$forename;
$company_name_method=str_replace(" ", "", $company);


if(strlen($name_method)<=5)
{
    $addition=rand(11,99);
    $first=$addition.$name_method;
}
else
{
    $first=substr($name_method,0,5);
}
if(strlen($company_name_method)<=5)
{
    $addition2=rand(11,99);
    $second=$addition2.$company_name_method;
}
else
{
    $second=substr($company_name_method,0,5);
}
$middle=rand(100,1000);

$username=$first.$middle.$second;
return($username);
}

Check Username Function:

check_user($user1, $dbc, $fname, $company);

function check_user($user1, $dbc, $surname, $company){
$check_username="SELECT username FROM is_user_db WHERE username='$user1'";
$resultx=mysqli_query($dbc, $check_username) or die("Could not check username");
$num_rows=mysqli_num_rows($resultx);
if($num_rows>0)
{
    $user1=create_username($fname, $company);
    check_user($user1, $dbc, $fname, $company);

}
else
{
    return($user1);
}
}

It just seems to return the original username.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T09:38:51+00:00Added an answer on June 8, 2026 at 9:38 am

    You probably need to re-factor your code a little. Write out the steps on paper; that helps me. So far, I can see:

    1. You want to check a username is unique on form submission
    2. If it’s not, generate a new username

    So, check the username when your form is POSTed:

    <?php
        if (isset($_POST['submit'])) {
            if (username_unique($_POST['username'])) {
                // carry on processing form
            }
            else {
                $suggested_username = suggest_username($_POST['username']);
                // display form, with new suggested username?
            }
        }
    

    And then write your functions:

    <?php
        // following on from code from above
    
        function check_username($username) {
            // get database connection (I use PDO)
            $sql = "SELECT COUNT(*) AS count FROM users_tbl WHERE username = ?";
            $stmt = $pdo->prepare($sql);
            $stmt->execute(array($username));
            $row = $stmt->fetchObject();
            return ($row->count > 0); // if 'count' is more than 0, username already exists
        }
    
        function suggest_username($username) {
            // take username, and add some random letters and numbers on the end
            return $username . uniqid();
        }
    

    Hopefully this will help. Obviously it’ll need some modification to work in your set-up, but this is the general flow you’ll need.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I trying to write a simple function to call unmanaged code from managed code.
I am new to PHP and am trying to write a simple function that
I am trying to write simple function that checks to see if a designer
I am trying to write a simple Java function that will take a list
I'm trying to write a simple Vim function that takes the name of a
I'm trying to write a simple php function that will strip everything between two
I'm trying to write a simple function == compareGPA that compares between the GPA
I'm trying to write a simple test that creates an image element, checks the
I'm trying to write a simple Javascript(jQuery) function that randomly displays 6 Divs out
I'm trying to write a simple function in C that would calculate the difference

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.