douyin9987 2015-01-03 17:27
浏览 48
已采纳

从sql数据库创建简单的xml文件

i'm a newbee to sql code .I need help to generate a simple xml file of youtube id's from my sql database via php.

DESIRED OUTPUT

<videos>
<youtube media="http://www.youtube.com/watch?v=_hTiRnqnvDs" autoplay="false"></youtube>
<youtube media="http://www.youtube.com/watch?v=5wx0GfbC0BA" autoplay="false"></youtube>
<youtube media="http://www.youtube.com/watch?v=ERGrSQoY5fs" autoplay="false"></youtube>
</videos>

MY OUTPUT

<videos>
<youtube media="http://www.youtube.com/watch?v=![CDATA[$value]]" autoplay="true"></youtube>
<youtube media="http://www.youtube.com/watch?v=![CDATA[$value]]" autoplay="true"></youtube>
<youtube media="http://www.youtube.com/watch?v=![CDATA[$value]]" autoplay="true"></youtube>
</videos>

This is the output I get from a database query

SELECT key,value FROM jr_jryoutube_item_key WHERE key = "youtube_id"

   
----------  -----------
key            value
----------  -----------
youtube_id  3VVAzFlmeWc
youtube_id  Rr9SfJwctRg
youtube_id  ocOZLHyOSZw
youtube_id  n-rQDYNOCyA
youtube_id  VaQlSnII-Hc

Below is my attempt.It's generating an xml file but it's not reading the youtube id's.

 <?php

error_reporting(E_ALL);
     //database configuration
$_conf['jrCore_db_host'] = 'localhost';
$_conf['jrCore_db_port'] = '3306';
$_conf['jrCore_db_name'] = 'xxxxxx';
$_conf['jrCore_db_user'] = 'xxxxxx';
$_conf['jrCore_db_pass'] = 'xxxxxx';

       //connect to host
$con = mysqli_connect($_conf['jrCore_db_host'],$_conf['jrCore_db_user'],$_conf['jrCore_db_pass']);

      // Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }

      // select database

mysqli_select_db($con,$_conf['jrCore_db_name']);

echo "Connected successfully";

$myFile = "videos.xml";
$fh = fopen($myFile, 'wb') or die("can't open file");
$xml_txt .= '<videos>';

    // $query is an array with your values
$query = "SELECT `key`,`value` FROM `jr_jryoutube_item_key` WHERE `key` = 'youtube_id'";
 
$result = mysqli_query($con,$query);

if (!$result) {
    die('Invalid query: ' . mysqli_error($con));
}
if(mysqli_num_rows($result)>0)
{
    while($result_array = mysqli_fetch_assoc($result))
    
   {
       
      //loop through each key,value pair in row
      foreach($result_array as $key => $value)
        {          
         //embed the SQL data in a CDATA element to avoid XML entity issues
         $xml_txt .= '<youtube media="http://www.youtube.com/watch?v=![CDATA[$value]]" autoplay="true">';
         $xml_txt .= '</youtube>';  
          
         }
 
     }
}
   
$xml_txt .= '</videos>';
fwrite($fh, $xml_txt);
fclose($fh);
 ?>

</div>
  • 写回答

1条回答 默认 最新

  • dongshaidu2456 2015-01-03 17:47
    关注
    $query = mysql_query("SELECT `value` FROM `jr_jryoutube_item_key` WHERE `key` = "youtube_id"");
    
    // $query is an array with your values
    
    // sprintf link template, %s is where youtube_id goes
    $sprintf_link = '<youtube media="http://www.youtube.com/watch?v=%s" autoplay="true">';
    
    // iterate the query row-wise
    foreach($query as $row_idx => $row)
    {  
        // fetch value
        $youtube_id = $row['value']; 
    
        // build link
        $xml_txt .= sprintf($sprintf_link, $youtube_id);  
    }
    

    This doesn't answer the part, where you concatenate / add the CDATA parts. It's just additional data from the db, which you might concatenate to the string or insert with sprintf().

    If the SQL query is true, then your DB table structure can be optimized. You a querying and replicating the string "youtube_id" over all rows. You might use a table "youtube_id" with "index" (auto_increment) and "key" (string length 11).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大