Re: Problem with inserting numbers...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Replies below


Jason Pruim
lists@xxxxxxxxxxxxxxxxxxxx



On Aug 10, 2011, at 11:08 PM, Ken Robinson wrote:

> At 09:22 PM 8/10/2011, Jason Pruim wrote:
>> So here I am attempting to generate some numbers to be inserted into a database... eventually they will make up a phone number (Which I've emailed about before and know about the bad ideas with it.. But it's the customer :))
>> 
>> Here is the code I am working with:
>> 
>> <?PHP
>> function number_pad($number,$n) {
>> return str_pad((int) $number,$n,"0",STR_PAD_LEFT);
>> }
>> 
>> $SQL = "SELECT * FROM Test WHERE `areacode` = '907' AND `prefix` = '200' LIMIT 5";
>> 
>> $result = mysql_query($SQL);
>> 
>> while ($num != "10000") {
>>    while($row = mysql_fetch_assoc($result)) {
>>        $padnum = number_pad($num, "4");
>>        echo $row['areacode'] . "-" . $row['prefix'] . "-" . $padnum . "<BR>";
>>        $num++;
>>    }
>> 
>> 
>> }
>> 
>> ?>
> 
> Try to avoid putting a database query in a loop.

But that's exactly what I need to do...  onceI have the code working with the echo I need to update the database with the numbers being displayed...

$fullnumber = $row['areacode'].$row['prefix'].$padnum;

$SQL = INSERT INTO Test ('fullnumber'( VALUES($fullnumber);


So I want that to be in a loop since it will be inserting roughly 10,000 records for every areacode/prefix combination :)


> In the query only ask for the fields you are going to use, it's faster. Why use your own function to pad a string, when there is a built-in function?

Because when I was doing my searching I came across the function I'm using before I saw the range function :)

> 
> Try this code (untested):
> <?php
> $nums = range(0,9999);
> $q = "SELECT areacode, prefix FROM Test WHERE `areacode` = '907' AND `prefix` = '200' LIMIT 5";
> $rs = mysql_query($q);
> while ($row = mysql_fetch_assoc($rs)) {
>        foreach ($nums as $n) {
>                echo sprintf('%03d-%03d-%04d',$row['areacode'],$row['prefix'],$n) "<br>\n";
>        }
> }
> ?>
> 

I will try this later today after the day job gets done...

Thanks for the help!



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux