I need A code so that It genorates a random code from a database and once its been genorated it gets deleted from the database.
Anyhelp will be much loved and also if Its possible to get it 1 per Ip every 24 hours thatll be even more loved ( I know proxies and stuff but I dont really care too much)
05-12-2008, 09:46 AM
carrotderek
What do you mean by "random code" and are you going to be managing your own database? I'll assume that since you are working with databases, you will be doing SQL queries and by "random code" you mean a random row from a table in your database. But I have to ask, are you going to be continually populating your db? Since you are deleting entries every time you generate one randomly.
Anyway, heres the code to generate a random entry and then delete it in PHP
Every table has a unique identifier known as a primary key. In this case, the primary key is the uid.
Code:
<?php
//connection information
$connection = mysql_connect ("<host>","<username>r", "<password>") OR DIE ("Cannot make the connection");
//connect to database and select
$db = mysql_select_db ("<databse>",$connection) OR DIE ("Cannot connect to database");
//get a random row from a table (note: this will select more than 1 column, you will have to define variables for each column to display them)
$query="SELECT * FROM <table> ORDER BY RAND() LIMIT 1";
$result = mysql_query($query);
//checks if your query brings up the row you want
if(mysql_num_rows($result)>0){
//return the row as an array -- this is how we assign each column to a variable
$row = mysql_fetch_array($result);
//$row is the array, get the elements inside the array
$username = $row['username'];
$uid = $row['uid'];
//test some output
echo $username;
//delete what you generated from the database.
$query = "DELETE FROM <table> WHERE uid='$uid'";
mysql_query($query) OR DIE ("could not remove entry from db");
}//end if
?>
05-21-2008, 03:54 PM
Lewk
what i mean is in my database say i have
abcdefg
efghsei
egrilngrf etc.
when one of them is genorated I want it to be delted after it is seen
05-22-2008, 07:54 PM
carrotderek
In that case, there is no need to have a database of codes if the data in it is going to be volatile. It's redundant to store volatile data in a database. You can just use the random function that is built into PHP.
Just call function like this:
Code:
<?php
function generateCode ($length){
$letters = "abcdefghijklmnopqrstuvxyz";
$string .= $letters;