Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
| Search | Today's Posts | Mark Forums Read
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old March 9th, 2004, 05:41 PM
Authorized User
 
Join Date: Dec 2003
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default random numbers from/to database

Hi,

could someone teach me how to random create a 10 digit number, and check to see if if exists in Mysql database field. if it does exist do a loop, if it doesn't, write to the DB.

Thanks!!
  #2 (permalink)  
Old March 9th, 2004, 06:40 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Why does it have to be random?

You can do an auto-increment on the field.

Create table with auto increment field. INT data type, 10 length.

ALTER TABLE table_name AUTO_INCREMENT = 999999999;

The next insert will be a 10 digit number and each subsequent will increment 1 from that one.

hth,
Rich


:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
  #3 (permalink)  
Old March 9th, 2004, 06:51 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Also, should have mentioned this before, but the query I provided just sets the integer value of the AUTO_INCREMENT field, it doesn't give the field itself AUTO_INCREMENT capability.

Here's an example of the CREATE TABLE query, which will set the AUTO_INCREMENT value with the creation of the table.

CREATE TABLE `submissions` (
  `submission_id` int(11) NOT NULL auto_increment,
  `user_id` int(11) NOT NULL default '0',
  `date` text NOT NULL,
  `category_id` int(11) NOT NULL default '0',
  `title` text NOT NULL,
  `body` mediumtext NOT NULL,
  `permission` int(1) NOT NULL default '0',
  `lastaccesstime` timestamp(14) NOT NULL,
  PRIMARY KEY (`submission_id`)
) TYPE=MyISAM AUTO_INCREMENT=999999999 ;

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
  #4 (permalink)  
Old March 12th, 2004, 04:29 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default


Well, the largest 4-byte integer you can store is 4294967295, so that limits the upper bound on your 10-digit numbers.

use rand() to generate numbers. You can specify an upper and lower bound. You can also use mt_rand() to do this -- many people prefer mt_rand over rand().


<?php

// connect to db here

do
{
    $num = mt_rand(1000000000, 4294967295);
    $result = mysql_query("SELECT count(*) FROM table WHERE col='$num'");
    if (! $result)
    {
        die("Query failed: MySQL said: " . mysql_error());
    }

    $count = mysql_result($result, 0);
} while ($count > 0);

mysql_query("INSERT INTO table (col) VALUES ('$num');


Does this make sense? If not, read about do..while loops here:

 http://www.php.net/do.while

Take care,

Nik
http://www.bigaction.org/
  #5 (permalink)  
Old March 13th, 2004, 11:05 AM
Authorized User
 
Join Date: Dec 2003
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Nikolai!

Well, that seemed to work, despite an error
<?php require_once('includes/config.php');

do
{
    $num = mt_rand(1000000000, 4294967295);
    $result = mysql_query("SELECT * FROM random WHERE nummer='$num'");
    if (! $result)
    {
        die("Query failed: MySQL said: " . mysql_error());
    }

    $count = mysql_result($result, 0);
} while ($count > 0);

mysql_query("INSERT INTO random (nummer) VALUES ('$num')");

?>

the red line produces the following err:

Warning: Unable to jump to row 0 on MySQL result index 12 in /var/www/html/random.php on line 22

the rest is working fine, Thanks!

HammR
  #6 (permalink)  
Old March 17th, 2004, 09:27 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Look more closely at your code -- you didn't copy my query correctly.

I select the _NUMBER_ of rows where the id field matches the number. You select ALL the rows where the id field matches the number. This is a huge difference, since my query guarantees a single result row with a single result column.

Your query might not return ANY rows, which is why mysql_result() is choking -- you can't select a value from a nonexistent row.


Take care,

Nik
http://www.bigaction.org/
  #7 (permalink)  
Old March 18th, 2004, 04:46 PM
Authorized User
 
Join Date: Dec 2003
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, its OK now, didn't know about the count(*) stuff :D

Thanks anyway!

Regards

HammR

  #8 (permalink)  
Old January 13th, 2005, 02:06 PM
Authorized User
 
Join Date: Jan 2005
Location: , , .
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

I am using php and mysql on Windoews 2000.
I have a problem storing a newly generated alphanumeric random number in a database table. I have the folowing code:

function rand1($length)
{
$Random = srand((double)microtime()*1000000);

$data = "AbcDE123IJKLMN67QRSTUVWXYZ";
$data .= "aBCdefghijklmn123opq45rs67tuv89wxyz";

for($i = 0; $i < $length; $i++)
{
$Random .= substr($data, (rand()%(strlen($data))), 1);
}

echo $Random;
}
rand1(10);

$TodaysDate = date("Y-m-d h:m:s");

$insert = "insert into tickettbl (TicketNo, RecDate)
    values ('$Random', '$TodaysDate')";

$result = mysql_query($insert);

if ($result)
echo "Entered";


Can anybody help me. I know it is something so simple i just can't figure it out.

Thanks in advanced,

Scoobie

  #9 (permalink)  
Old January 13th, 2005, 07:47 PM
Authorized User
 
Join Date: Jan 2005
Location: , , .
Posts: 82
Thanks: 0
Thanked 0 Times in 0 Posts
Default

add this and tell me what error you are getting, we might be able to track down more details this way:

$result = mysql_query($insert) or die(mysql_error());



  #10 (permalink)  
Old January 14th, 2005, 06:00 AM
Authorized User
 
Join Date: Jan 2005
Location: , , .
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

 i get this error: Duplicate entry '' for key 2

i have set ticketNo column to unique





Similar Threads
Thread Thread Starter Forum Replies Last Post
Generating random numbers invisibleman18 Java Basics 2 March 26th, 2007 12:22 AM
How to genarate Uniqe Random numbers KamalRaturi ASP.NET 1.0 and 1.1 Basics 0 September 24th, 2006 02:08 PM
Random Numbers Using MersenneTwister JoeDW C# 0 September 13th, 2006 11:10 AM
Random Numbers welshboy2005 C# 1 May 19th, 2005 06:01 AM
Random Numbers not so random... katsarosj ASP.NET 1.0 and 1.1 Basics 5 November 20th, 2003 12:55 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.