Current location: Hot Scripts Forums » Other Discussions » Database » Generate unique number then store


Generate unique number then store

Reply
  #1 (permalink)  
Old 10-16-06, 02:12 PM
phpfreek phpfreek is offline
Wannabe Coder
 
Join Date: Feb 2006
Posts: 113
Thanks: 0
Thanked 0 Times in 0 Posts
Generate unique number then store

Hello;
i am looking for a code that will generate a number composed out of 6 numbers(xxxxxx), the number must be random and unique ...

I don't want to use auto-increment ... i just want a php script that will generate a 6 number number that is unique in the hole database (no field in the database is equal to this number), and then store it in a given field in a given table ...


Is this possible using only php codes ?

If someone thinks it's possible and can code it, please give me the code, thank you in advance.
Best Regards;
PHPFREEK

Last edited by phpfreek; 10-16-06 at 02:15 PM.
Reply With Quote
  #2 (permalink)  
Old 10-16-06, 02:33 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Yes, me again, lol.

Untested but should work with a little modification in the query.
PHP Code:

function generate_unique_id()

{
    
$num '';

    for (
$i 0$i 6$i++)
    {
        
$num .= rand(09);
    }
    
    
$check mysql_query("SELECT number FROM table WHERE number = "$num) OR die( mysql_error() );
    
    if (
mysql_num_rows($check) == 0)
    {
        return 
$num;
    }
    else
    {
        return 
generate_unique_id();
    }
}

$unique_number generate_unique_id(); 
Reply With Quote
  #3 (permalink)  
Old 10-17-06, 08:37 AM
phpfreek phpfreek is offline
Wannabe Coder
 
Join Date: Feb 2006
Posts: 113
Thanks: 0
Thanked 0 Times in 0 Posts
OMG ... are you searching for everything i post and help me with it ? ... or is it coincidence ?

Anyway, i have edited the script to be like this :

PHP Code:

<?php 

session_start
();
include(
"config.php");
$usernamee=$_SESSION['username'];
function 
generate_unique_id() 

    
$num ''

    for (
$i 0$i 6$i++) 
    { 
        
$num .= rand(09); 
    } 
     
    
$check mysql_query("SELECT number FROM $usernamee WHERE tickets = "$num) OR die( mysql_error() ); 
     
    if (
mysql_num_rows($check) == 0
    { 
        return 
$num
    } 
    else 
    { 
        return 
generate_unique_id(); 
    } 


$unique_number generate_unique_id();

?>
Now this means that the table's name will be the username, and the field where the code must check if there is a number like it or not s called "tickets".

What your code does is generate the number and check if it exists (then it will start the process all over again).
But if the number doesn't exist, your code doesn't store the number in the field ... there's a query missing ... am i right or wrong ??

Waiting your reply.
Best Regards And thank again;
PHPFREEK
Reply With Quote
  #4 (permalink)  
Old 10-18-06, 07:27 AM
phpfreek phpfreek is offline
Wannabe Coder
 
Join Date: Feb 2006
Posts: 113
Thanks: 0
Thanked 0 Times in 0 Posts
still waiting ...
Reply With Quote
  #5 (permalink)  
Old 10-18-06, 09:14 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Try this.

PHP Code:

<?php 

session_start
();
include(
"config.php");
$usernamee=$_SESSION['username'];
function 
generate_unique_id() 

    
$num ''

    for (
$i 0$i 6$i++) 
    { 
        
$num .= rand(09); 
    } 
     
    
$check mysql_query("SELECT tickets FROM $usernamee WHERE tickets = "$num) OR die( mysql_error() ); 
     
    if (
mysql_num_rows($check) == 0
    { 
        
mysql_query("INSER INTO "$usernamee ." (tickets) VALUES ("$num .")") OR die( mysql_error() );
        return 
$num
    } 
    else 
    { 
        return 
generate_unique_id(); 
    } 


$unique_number generate_unique_id();

?>
Reply With Quote
  #6 (permalink)  
Old 10-18-06, 09:35 AM
phpfreek phpfreek is offline
Wannabe Coder
 
Join Date: Feb 2006
Posts: 113
Thanks: 0
Thanked 0 Times in 0 Posts
Hey bro, thanks for your reply

i ttried the code u gave me, below is the phrase that appeared whn i executed it :

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE tickets = 781930' at line 1"

Why is this ??

Thanks again ...
PHPFREEK
Reply With Quote
  #7 (permalink)  
Old 10-18-06, 05:58 PM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
instead of this:
PHP Code:

$check mysql_query("SELECT tickets FROM $usernamee WHERE tickets = "$num) OR die( mysql_error() ); 

try this:
PHP Code:

$check mysql_query("SELECT tickets FROM $usernamee WHERE tickets = '"$num "'") OR die( mysql_error() ); 

this should do it,
UnrealEd
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #8 (permalink)  
Old 10-18-06, 06:34 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Integers don't need quotes usually. That's why I left them out.
Reply With Quote
  #9 (permalink)  
Old 10-19-06, 02:45 AM
jfulton's Avatar
jfulton jfulton is offline
Community VIP
 
Join Date: Apr 2006
Location: Los Angeles, CA
Posts: 660
Thanks: 0
Thanked 0 Times in 0 Posts
It will need quotes if it's a string (Char/Varchar) though. Like the difference between '0013009' and 13009.
Reply With Quote
  #10 (permalink)  
Old 10-19-06, 11:01 AM
phpfreek phpfreek is offline
Wannabe Coder
 
Join Date: Feb 2006
Posts: 113
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for helping me out guys, but anther error is beeing displayed

Here is my script now :

PHP Code:

<?php  

session_start
(); 
include(
"config.php"); 
$usernamee=$_SESSION['username']; 
function 
generate_unique_id()  
{  
    
$num '';  

    for (
$i 0$i 6$i++)  
    {  
        
$num .= rand(09);  
    }  
      
    
$check mysql_query("SELECT tickets FROM $usernamee WHERE tickets = '"$num "') OR die( mysql_error() );  
      
    if (mysql_num_rows(
$check) == 0)  
    {  
        mysql_query("
INSER INTO ". $usernamee ." (ticketsVALUES (". $num .")") OR die( mysql_error() ); 
        return 
$num;  
    }  
    else  
    {  
        return generate_unique_id();  
    }  
}  

$unique_number = generate_unique_id(); 

?>
And here is the error being displayed :

Parse error: parse error, unexpected T_STRING in C:\Documents and Settings\Administrator\Desktop\xampplite\htdocs\xa mpp\test\FEDEX\change2.php on line 18

Will this script ever work ???

Thanks again
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Unique Number a.jounidov Script Requests 7 03-29-06 05:16 AM
Perfect number... Argo_Jeude C/C++ 1 07-14-05 02:35 PM
Generate form from unknown number of database fields? Tim Mousel PHP 2 07-11-05 08:30 PM


All times are GMT -5. The time now is 04:56 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.