help with php & mySql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Kofikoduah
Forum Newbie
Posts: 8
Joined: Tue Mar 21, 2006 7:45 am

help with php & mySql

Post 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

Code: Select all

$output
?

let's see:

Code: Select all

<?

$output;

echo $output;

?>

how do we move on from there?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

SELECT 1 FROM `table` WHERE `field` = '{$output}'
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Kofikoduah
Forum Newbie
Posts: 8
Joined: Tue Mar 21, 2006 7:45 am

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you'll need the select.
Kofikoduah
Forum Newbie
Posts: 8
Joined: Tue Mar 21, 2006 7:45 am

Post 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;
}
?>
Post Reply