在mysql数据库中保存函数

时间:2015-02-06 08:00:35

标签: php mysql

你好我写函数代码

我希望保存功能代码包含表html到mysql数据库

function writeMsg(){
  $result = mysql_query('SELECT * FROM `wp_farsc`');



  if($result && mysql_num_rows ($result)> 0){

      echo '<table border="2px" cellspacing="5px" width="100%">'.PHP_EOL;
      echo '<tr bgcolor="yellow"><th>سکه</th><th>قیمت انلاین</th><th>تغییر قیمت</th><th>کمترین</th><th>بیشترین</th><th>زمان بروز رسانی</th></tr>'.PHP_EOL;
      while($row = mysql_fetch_assoc($result)){

         echo'<tr>';
         echo '<td>'.$row ['titile'].'</td>';
         echo '<td>'.$row ['liveprice'].'</td>';
         echo '<td>'.$row ['changing'].'</td>';
         echo '<td>'.$row ['lowest'].'</td>';
         echo '<td>'.$row ['topest'].'</td>';
         echo '<td>'.$row ['time'].'</td>';
         echo'</tr>'.PHP_EOL;
      }

      }

  mysql_close();
  }

然后我将函数writeMsg转换为varible

     $oiobz1 = writeMsg(1, 'center', '', 1);

然后保存到数据库

  @mysql_connect("localhost","root","") or die('connection error');
  mysql_select_db('gas') or die('Can not connect to the database');
    @ mysql_query("set names utf8");
  $title ="today list";
  $content =" writemsg()";
  $statuse="Publish";
  $pingo="open";
  $commento="open";
  $typee="post";
  $auther="1";
  $sql = "INSERT INTO `wp_posts`(   post_author,post_title,post_content,post_status,post_type,ping_status,comment_status) VALUES('$auther','$title','". $oiobz1."','$statuse','$typee','$pingo','$commento')";
   @ mysql_query($sql);

将数据保存到dabase后,所有字段都与post_content分开存储.. post_content为null ..

http://i.stack.imgur.com/YgGX7.jpg

1 个答案:

答案 0 :(得分:1)

你必须使用函数返回; 尝试使用代码

 function writeMsg(){
  $result = mysql_query('SELECT * FROM `wp_farsc`');



  if($result && mysql_num_rows ($result)> 0){

      echo '<table border="2px" cellspacing="5px" width="100%">'.PHP_EOL;
      echo '<tr bgcolor="yellow"><th>سکه</th><th>قیمت انلاین</th><th>تغییر قیمت</th><th>کمترین</th><th>بیشترین</th><th>زمان بروز رسانی</th></tr>'.PHP_EOL;
     $data = null;
      while($row = mysql_fetch_assoc($result)){

         $data .='<tr>';
         $data .= '<td>'.$row ['titile'].'</td>';
         $data .='<td>'.$row ['liveprice'].'</td>';
         $data .='<td>'.$row ['changing'].'</td>';
         $data .= '<td>'.$row ['lowest'].'</td>';
         $data .= '<td>'.$row ['topest'].'</td>';
         $data .= '<td>'.$row ['time'].'</td>';
         $data .='</tr>'.PHP_EOL;
      }

      }

  mysql_close();
  return $data;
  }