user3584968
user3584968

Reputation: 353

Generate a random 10 digit number and insert into mysql?

I am trying to generate a random 10 digit number and insert this into mysql on page load, so each time the page refreshes or loads there should be a different 10 digit number being inserted into the database, however at the moment it just keeps inserting the same 10 digit number again and again.

Can someone please show me where I am going wrong? Thanks.

<?php
    session_start();
    $db_hostname = 'localhost';
    $db_database = 'hewden1'; 
    $db_username = 'root';
    $db_password = '';
    $db_server = mysql_connect($db_hostname, $db_username, $db_password)    
            or die("Unable to connect to MySQL: " . mysql_error());
    mysql_select_db($db_database)   
    or die("Unable to select database: " . mysql_error());

    $num0 = (rand(10,100));
    $num1 = date("Ymd");
    $num2 = (rand(100,1000));
    $num3 = time();
    $randnum = $num0 . $num1 . $num2 . $num3;


    $sql="INSERT INTO supplier_session (session_number)
    VALUES ('$randnum')";

    $result = mysql_query($sql); 
    ?>

This is the number I am constantly getting: 2147483647

Upvotes: 8

Views: 55944

Answers (12)

Umair Hanif
Umair Hanif

Reputation: 174

It is the most suitable method to remember that when you did generate this key/number

echo $key = time(); 

its 10 digit number that convert this into exact date and time.

Upvotes: 0

Kaustav Chakravorty
Kaustav Chakravorty

Reputation: 260

Here is the simple way of generating 10 digit random number

<?php

function generateRandomNumber($length = 10) {
    $number = '1234567890';
    $numberLength = strlen($number);
    $randomNumber = '';
    for ($i = 0; $i < $length; $i++) {
        $randomNumber .= $number[rand(0, $numberLength - 1)];
    }
    return $randomNumber;
}

echo generateRandomNumber();

?>

Upvotes: 0

Wil
Wil

Reputation: 36

When I run your script I get different numbers everytime. Why don't you use the uniqid() function ?

Upvotes: 0

Professor
Professor

Reputation: 628

You can also try this if you want. Its not generating a random number but a random string

private string generaterandomnumber()
        {
            string Rand1 = RandomString(8);
            string Rand2 = RandomString(8);
            docNum = Rand1 + "-" + Rand2;
            MD5 md5 = new MD5CryptoServiceProvider();
            md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(docNum));

            //get hash result after compute it
            byte[] result = md5.Hash;

            StringBuilder strBuilder = new StringBuilder();
            for (int i = 0; i < result.Length; i++)
            {
                //change it into 2 hexadecimal digits
                //for each byte
                strBuilder.Append(result[i].ToString("x2"));
            }
            return strBuilder.ToString();
        }

Downvoting not required!!! Thx

Upvotes: 2

Vasil Nikolov
Vasil Nikolov

Reputation: 1120

You need to change column type. The number that you try to insert is higher than column maximum value (in your case SIGNED INT - 2147483647)

SIGNED INT - 2147483647
UNSIGNED INT - 4294967295

SIGNED BIGINT - 9223372036854775807
UNSIGNED BIGINT - 18446744073709551615

Upvotes: 5

Danieloplata
Danieloplata

Reputation: 222

There is no problem with your number generating code. Try unsetting your variables before they are defined and see if it fixes the problem.

unset($num0, $num1, $num2, $num3);
$num0 = (rand(10,100));
$num1 = date("Ymd");
$num2 = (rand(100,1000));
$num3 = time();
$randnum = $num0 . $num1 . $num2 . $num3;

You could just as easily use the rand function to generate a 10 digit number by using the following code: rand(1111111111,9999999999)

Upvotes: 0

Kami
Kami

Reputation: 19427

You should consider using the mt_rand function as gives a better distribution range.

In your code, you are not using the full 10 digit number range, try something like

$MIN_SESSION_ID = 1000000000;
$MAX_SESSION_ID = 9999999999;

$randId = mt_rand($MIN_SESSION_ID, $MAX_SESSION_ID);

echo $randId;

Upvotes: 1

h2ooooooo
h2ooooooo

Reputation: 39540

You're getting 2147483647 because it's the highest signed 32 bit integer that exists (binary it's 1111111111111111111111111111111). This is also the contents of the constant PHP_INT_MAX.

Just use a string with a loop and the mt_rand() function:

<?php

for ($randomNumber = mt_rand(1, 9), $i = 1; $i < 10; $i++) {
    $randomNumber .= mt_rand(0, 9);
}

var_dump($randomNumber); //eg. string(10) "9152676628"

DEMO

Upvotes: 3

alreadycoded.com
alreadycoded.com

Reputation: 326

function random_num ($len)
{
 $ch = "0123456789";
 $l = strlen ($ch) - 1;
 $str = "";
 for ($i=0; $i < $len; $i++)
 {
     $x = rand (0, $l);
     $str .= $ch[$x];
 }
 return $str;
}

Upvotes: 0

dkakoti
dkakoti

Reputation: 667

Use php mt_rand() function instead of rand()

<?php
echo mt_rand();
?>

Upvotes: 0

Rakesh Sharma
Rakesh Sharma

Reputation: 13728

try to getting ten digit rand no

$randnum = rand(1111111111,9999999999);

Upvotes: 41

Manibharathi
Manibharathi

Reputation: 945

Use this code.

$x = 10; // Amount of digits
$min = pow(10,$x);
$max = pow(10,$x+1)-1);
$value = rand($min, $max);

Upvotes: 0

Related Questions