Re: Problem with inserting numbers...
| From: | Ken Robinson | Date: | Thu, 11 Aug 2011 03:08:40 +0000 |
| Subject: | Re: Problem with inserting numbers... | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-314481@lists.php.net to get a copy of this message | ||
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 WHERETry to avoid putting a database query in a loop. 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? Try this code (untested): <?php $nums = range(0,9999); $q = "SELECT areacode, prefix FROM Test WHEREareacode= '907' ANDprefix= '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++; }} ?>
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";
}
}
?>
Ken