当前位置: 代码迷 >> PHP >> php文件小修改,该如何处理
  详细解决方案

php文件小修改,该如何处理

热度:245   发布时间:2016-04-29 01:31:13.0
php文件小修改
一个读取数据库内容添加到txt里面的php文件,想要在读取的内容前面或者后面加上内容,该怎么写?比如"前缀+内容+后缀"
加号是省略的
PHP code
<?php $cn = mysql_connect('localhost','root','');if( $cn ){ $files=glob("*.txt"); foreach($files as $file)   unlink($file);mysql_connect("106.187.43.139", "caijizhi_123", "7898585") or        die("Could not connect: " . mysql_error());    mysql_select_db("caijizhi_123");    $result = mysql_query("SELECT title FROM cjx_archives") or die(mysql_error());            $file_index=1;   $i=1;    while ($row = mysql_fetch_array($result, MYSQL_NUM)) {        if(! ($i % 150) ) $file_index++;$i++;         file_put_contents ( $file_index.'.txt' , $row[0].  PHP_EOL, FILE_APPEND );      }    mysql_free_result($result);    }else{ die('连接失败');}?>


------解决方案--------------------
PHP code
//先定义好 $prefix, $suffix的值,说白了就是字符串连接问题。。你能写这些代码,字符串连接不会?file_put_contents ( $file_index.'.txt' , $prefix . $row[0] . $suffix . PHP_EOL, FILE_APPEND );
  相关解决方案