doujiao8649 2009-10-14 16:21
浏览 42
已采纳

使用PHP将MySQL中的行解析为文件无法正常工作

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);
    $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?

  • 写回答

2条回答 默认 最新

  • doupang1917 2009-10-14 16:28
    关注

    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c