Jump to content


Photo

random number


  • Please log in to reply
5 replies to this topic

#1 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 03 March 2006 - 12:04 PM

So what i am trying to do is get a random number to be in the database and change each time a user goes to that page via the update statement but also show the user that random number.

hopefully if you press the refresh button a random number will come up diffrent but also entred in the database to one colum thanks.

This will display a 8 digit number.
<?php
//post rand_pass
$rand_pass=$_POST['var'];

// create a random number

$length    = 8;
$key_chars = '0123456789';
$rand_max  = strlen($key_chars) - 1;

for ($i = 0; $i < $length; $i++)
{
   $rand_pos  = rand(0, $rand_max);
   $rand_key[] = $key_chars{$rand_pos};
}

$rand_pass = implode('', $rand_key);

// show the random number to the user.
echo $rand_pass;

// insert into the database the first random number

$query = "insert into  valadate values( '$var')";
$results = mysql_query($query) or die("Failed on query!");


// check the number in the database to the one displayed and update with new number.

else{

$check_ver=mysql_query("SELECT * FROM valadate WHERE var='$rand_num' ");

if(mysql_num_rows($check_var = '$rand_num') ){

UPDATE valadate SET var = $rand_num; 
} 
}

?>



Someone kindly give an example how to update a random number on a singles field and then display it and make it change on one field in the database and also change the number each time the page is called.
advance thank you.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#2 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 03 March 2006 - 01:45 PM

[code=auto:0]
do {
$random_min = 1; // change to your min
$random_max = 200; // change to your max.

$rand_no = rand($random_min, $random_max);

$updatedb = mysql_query("UPDATE `validate` SET `var` = $rand_num!);
} while(mysql_affected_rows() === 0);

echo $rand_no;
follow me on twitter @PHPsycho

#3 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 03 March 2006 - 01:59 PM


thank you for the code but i got a error please help someone cheers.


This line.
} while(mysql_affected_rows() === 0);





<?

$sql_host =   'xxx';  //add host information here (localhost, mysql.host.com, etc.)
$sql_un   =   'xxx';  //add your user name here
$sql_pass =   'xxx';  //add your password here
$sql_db   =   'valadate';  //add your database name here


mysql_connect("$sql_host", "$sql_un", "$sql_pass") or
    die ("Could not connect to database");
mysql_select_db($sql_db) or

    die ("Could not select database");

do {
$random_min = 1; // change to your min
$random_max = 200; // change to your max.

$rand_no = rand($random_min, $random_max);

$updatedb = mysql_query("UPDATE `validate` SET `var` = $rand_num!);
 } while(mysql_affected_rows() === 0);


echo $rand_no;
?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#4 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 03 March 2006 - 02:03 PM

sorry try...

} while (mysql_affected_rows() < 1);
follow me on twitter @PHPsycho

#5 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 03 March 2006 - 02:19 PM

How can i get this to update when you enter a page automatickly thank you so much.
<?

$sql_host =   'xxx';  //add host information here (localhost, mysql.host.com, etc.)
$sql_un   =   'xxx';  //add your user name here
$sql_pass =   'xxx';  //add your password here
$sql_db   =   'valadate';  //add your database name here


mysql_connect("$sql_host", "$sql_un", "$sql_pass") or
    die ("Could not connect to database");
mysql_select_db($sql_db) or

    die ("Could not select database");



do {
$random_min = 1; // change to your min
$random_max = 2; // change to your max.

$rand_no = rand($random_min, $random_max);

$updatedb = mysql_query("UPDATE `valadate` SET `var` = $_post($rand_no!)");
 } while(mysql_affected_rows() === 0);


echo $rand_no;
?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#6 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 03 March 2006 - 03:42 PM

right sorry...

thought you had set up a table on your database called validate - but its the actual database that is called validate!!!

ok in the query line

$updatedb = mysql_query("UPDATE `valadate` SET `var` = $_post($rand_no!)");

change that to this

$updatedb = mysql_query("UPDATE `YOURTABLE` SET `FIELD` = $rand_no");

(don't know why you have that $_post($rand_no!) in - it won't work!)

now you need to replace TABLE with the name of the database table you are using to store this in, and FIELD by the name of the field in that table that you are storing this in.
follow me on twitter @PHPsycho




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users