Parsing rows from MySQL into files using PHP is not working
I'm trying to parse an entire MySQL table into files named after the rows. However, it seems to miss out a lot of files, and it just isn't working properly, but I don't understand what's wrong. Here's what I'm using:
$link = mysql_connect(...); //*Let's assume this is correct*
mysql_select_db(*This too*);
$query = "SELECT * FROM rules";
$result = mysql_query($query) or die(mysql_error());
$num = mysql_num_rows($result) or die(mysql_error());
for($x = 0;$x < $num;$x++) {
$rules = mysql_fetch_assoc($result开发者_StackOverflow);
$file = '';
if($rules['except'])
$file .= $common = str_replace(explode(',', $rules['except']), "", $common);
if($rules['custom'])
$file .= $rules['custom'];
if($rules['css'])
$file .= trim($rules['css'], ";")."{display:none !important;height:0px !important;width:0px !important;}";
if($file == '') {
$handle = fopen($rules['site'].'.css', 'w');
fwrite($handle, $file);
}
}
Can anyone see anything wrong?
Looks like the problem you're searching is in this line if($file == '') {
. It will be true only if $file
is empty. But I think you need an oposite condition like following:
if($file != '') {
Also I suggest you to always close the file you've opened:
if($file != '') {
$handle = fopen($rules['site'].'.css', 'w');
fwrite($handle, $file);
fclose($handle);
}
if i am not mistaken by looking at it, seems like this line:
$file .= $common = str_replace(explode(',', $rules['except']), "", $common);
would always be empty.
精彩评论