Page 1 of 1
help with php & mySql
Posted: Wed Mar 22, 2006 6:44 pm
by Kofikoduah
Hello people, i have a problem. i have a random number generator which does its job generate random 15 digit numbers in php. what i want to do is post the output number of the the php function to a mysql database. That may sound easy but i want to also check that the outputed number does not already exist in the database. how do i achieve that if we assume the outputed number is called
?
let's see:
Code: Select all
<?
$output;
echo $output;
?>
how do we move on from there?
Posted: Wed Mar 22, 2006 7:43 pm
by feyd
Code: Select all
SELECT 1 FROM `table` WHERE `field` = '{$output}'
Posted: Wed Mar 22, 2006 7:47 pm
by Todd_Z
if the "number" field is a primary key [ or unique key? (idk) ] then you could use
Code: Select all
REPLACE INTO `table` ( `number` ) VALUES ( '{$output}' );
This will insert the value in the table if it isn't already there, if it is there, then it wont do anything.
You may want to explain the context of your problem a little more to get more detailed/specific help.
Posted: Wed Mar 22, 2006 7:50 pm
by feyd
I would expect that the random number would need to be regenerated if it existed, so I don't think REPLACE will be very useful.
Posted: Wed Mar 22, 2006 8:29 pm
by Kofikoduah
True, i would like the number to be regenerated if it exists. The function would just be called again... So do i use
Code: Select all
SELECT 1 FROM `table` WHERE `field` = '{$output}'
or
Code: Select all
REPLACE INTO `table` ( `number` ) VALUES ( '{$output}' );
?
Thanx for the quick reply
Posted: Wed Mar 22, 2006 8:33 pm
by feyd
you'll need the select.
Posted: Wed Mar 22, 2006 8:41 pm
by Kofikoduah
Ok, thanx for the repy, i'm basically starting an online service in my country where registered users can purchase goods online from member merchants of the service. Currently no such thing exists in my country.
Basically $output4capture is what is generated and i need it to be sent to a database along with other user details. The only thing is that this is a behind the scene operation which the user never notices until his details are emailed and card with the generated number is posted to him/her. so as originally stated, i want to validate the generated number against vaild entries in the database for duplicates. The generation process a function which can be called again if the number already exists.
Code: Select all
<?
function generate_Ashanti(){
//---------Defines Onpay Cards-----------------
//15-digit cards, Arranged by Geographic Location in Ghana
//Ashanti Region
$onpayPrefixList[] = "2029";
$onpayPrefixList[] = "3029";
/*
'prefix' is the start of the number as a string, any number of digits.
'length' is the length of the number to generate. Typically 15 or 16
*/
function completed_number($prefix, $length) {
$ccnumber = $prefix;
# generate digits
while ( strlen($ccnumber) < ($length - 1) ) {
$ccnumber .= rand(0,9);
}
# Calculate sum
$sum = 0;
$pos = 0;
$reversedCCnumber = strrev( $ccnumber );
while ( $pos < $length - 1 ) {
$odd = $reversedCCnumber[ $pos ] * 2;
if ( $odd > 9 ) {
$odd -= 9;
}
$sum += $odd;
if ( $pos != ($length - 2) ) {
$sum += $reversedCCnumber[ $pos +1 ];
}
$pos += 2;
}
# Calculate check digit
$checkdigit = (( floor($sum/10) + 1) * 10 - $sum) % 10;
$ccnumber .= $checkdigit;
return $ccnumber;
}
function credit_card_number($prefixList, $length, $howMany) {
for ($i = 0; $i < $howMany; $i++) {
$ccnumber = $prefixList[ array_rand($prefixList) ];
$result[] = completed_number($ccnumber, $length);
}
return $result;
}
function output($title, $numbers) {
$result[] = "<div class='ONPAYNumbers'>";
$result[] = "<h3>$title</h3>";
$result[] = implode('<br />', $numbers);
$result[]= '</div>';
return implode('<br />', $result);
}
#
# Main
#
echo "<div class='creditCardSet'>";
$onpay = credit_card_number($onpayPrefixList, 15, 1);
$out4capture = output("ONPAY CARD FOR ASHANTI REGION", $onpay);
//----Print's the generated number on screen
echo $out4capture;
}
?>