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 6215173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:00:27+00:00 2026-05-24T07:00:27+00:00

This is somewhat a continuation of this post I’ve taken the following code and

  • 0

This is somewhat a continuation of this post

I’ve taken the following code and somewhat adapted it to my needs;

function makeString() {

    // Create our random string
    $string = "";
    $characters = array('a', 'b', 'c', 'd');

    for ($i = 0; $i < 4; $i++) {

        $string .= $characters[mt_rand(0, 4)];
    }

    $query = "SELECT COUNT(*)
    FROM myTable
    WHERE string = '{$string}'";

    $result = mysql_query($query);
    $row = mysql_fetch_assoc($result);

    if ($row['COUNT(*)'] > 0) { // if it already exists, do it again

        $string = makeString();
    }

    return $string;
}

My version:

function valCode() {

$num1 = mt_rand(1, 6);
$valcode = $num1;

include("dbdata.php");
$conn = mysql_connect($db_host, $db_uname, $db_pword) or die("Couldn't connect because ".mysql_error());
mysql_select_db($db_name);
$query = "SELECT COUNT(*) FROM records WHERE valcode='{$valcode}'";
$result = mysql_query($query) or die("Query failed because ".mysql_error());

$row = mysql_fetch_assoc($result);

if($row['COUNT(*)'] > 0) {
    $valcode = valCode();
}

$query2 = "INSERT INTO records (valcode) VALUES ($valcode)";
$result2 = mysql_query($query2) or die("Query failed because ".mysql_error());

return $valcode;
}

valCode();

Rather than creating a random string (an int in the example), checking it against the database, then running it again if found, this runs the script twice, inserting duplicate values into my table.

I thought perhaps this could because of the use of a Primary Key allowing two of the same values to be placed in under different Primary Keys, thus ID’ing them as uniques, but in truth I really have no idea.

Any help or comments would be very much appreciated!

  • 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-05-24T07:00:28+00:00Added an answer on May 24, 2026 at 7:00 am

    It’s doing what your code says to do. Every call to valCode() will execute one INSERT query because that’s part of the function.

    An example run-through of your code:

    • You call valCode() to start looking for a new code
    • valCode() generates a code and finds it already exists, so it calls valCode() again
    • valCode() generates a code and finds it already exists, so it calls valCode() again
    • valCode() generates a unique code, INSERTs it into the database, and returns to its caller (the second call of the function)
    • The second call of valCode now INSERTs the code and returns to its caller (the first call of the function)
    • The first call of valCode now INSERTs the code and returns to its caller (the end of your code)

    You need to move the INSERT outside of the function and use its return value in the query.

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

Sidebar

Related Questions

No related questions found

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.