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 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi