简体   繁体   English

在xml文件中回显php代码

[英]echo php code in xml file

its me again with yet another query.If this is going to be too complexed then i wont even bother...so heres the query. 它再次与另一个查询。如果这将太复杂,那么我什至不会打扰...所以这里查询。

I have the title,artist,name of the person & dedication message stored in mysql.May be this is stupid..is it possible to create xml file to retrieve the above four information from the database. 我的标题,艺术家,人员名称和奉献消息存储在mysql中。可能是愚蠢的。是否可以创建xml文件从数据库中检索上述四个信息。 I have coded the below on the xml file but not sure whats missing.The config.php has database connection information. 我已经在xml文件中编码了以下内容,但不确定丢失了什么。config.php具有数据库连接信息。

Many thanks for your help!!!! 非常感谢您的帮助!!!! Nev 内夫

    <?php

require("config.php");



$i=0;
$db = "SELECT * FROM songlist WHERE songtype='S' ORDER BY date_added DESC LIMIT 50";
$db = "SELECT songlist.artist, songlist.title, requestlist.name, requestlist.msg FROM songlist, requestlist WHERE requestlist.name <> '' and requestlist.songID = songlist.ID ORDER BY requestlist.t_stamp DESC LIMIT 20";
$count = 1;

    while($results = $db->row())
    {
      $count++;

      if(($count % 2)== 0)

?>
<?php echo('<?xml version="1.0" encoding="utf-8"?>'); ?>
<playlist version="1" xmlns="http://xspf.org/ns/0/">

<trackList>
  <track>

        <Song><?php echo $results['title']?></Song>
        <album><![CDATA[<?php echo $results['artist']; ?>]]></album>
    </track>

    </trackList>

</playlist>

<?
$i++;
}
?>

Okay, your primary problem seems this: 好的,您的主要问题似乎是这样的:

$db = "SELECT * ..";
$db = "SELECT song ...";

    while($results = $db->row())
    {

You are assigning a string there, and immediately afterwards try to use it as some sort of database interface. 您正在那里分配一个字符串,然后立即尝试将其用作某种数据库接口。 Since I have no idea what is going on in your config script, or what weird database interface you use, it's easier explained with using the outdated mysql_functions: 由于我不知道您的配置脚本中发生了什么,或者您使用的是什么奇怪的数据库接口,因此使用过时的mysql_functions可以更轻松地进行解释:

$result = mysql_query("SELECT songlist.artist, ... goes here");

while($results = mysql_fetch_assoc($result))
{

The mysql_query runs your SELECT statement, and only then can you iterate over the results with mysql_fetch_assoc using the $result value that you got from mysql_query. mysql_query运行SELECT语句,然后才可以使用从mysql_query获得的$ result值对mysql_fetch_assoc进行迭代。

Note that you have to decide which of your two queries to actually run here. 请注意,您必须决定要在此处实际运行两个查询中的哪一个。 You cannot use two SELECT queries concurrently. 您不能同时使用两个SELECT查询。 (Or not not in your case at least.) (或者至少不是您的情况。)

You haven't explained what your problem is? 您还没有解释您的问题是什么? Also, I would need information on the format required for the xml, or what commands you'd need to send to your database code. 另外,我将需要有关xml所需格式的信息,或者需要将哪些命令发送到数据库代码的信息。

It looks like you have some formatting errors as well. 看来您也有一些格式错误。 I can only guess at what would work, but try this: 我只能猜测会起作用的方法,但可以尝试以下方法:

1) At the beginning of the file, you seem to have forgotten the start tag 2) $i seems to be unused, delete it (?) 3) Your SQL statement is not only unenclosed in quotes, but would need to be sent to the database code (a complete guess since I don't know what's inside your config.php would be $db->query("your sql here"); 4) delete: '); 1)在文件的开头,您似乎已经忘记了开始标记2)$ i似乎未使用,请将其删除(?)3)您的SQL语句不仅未用引号引起来,而且还需要发送给数据库代码(一个完整的猜测,因为我不知道您的config.php里面有什么是$ db-> query(“您的sql在这里”); 4)删除:'); ?> 5) ... actually this is getting silly, I should probably just rewrite it for you!!! ?> 5)...实际上,这变得越来越愚蠢,我可能应该为您重写它!!!

The thing is, before anything else, I'd need to know If you NEED to use the config.php? 问题是,首先,我需要知道是否需要使用config.php? Or if I could code using native php functions. 或者,如果我可以使用本地php函数进行编码。

But this is still fraught with problems (since I don't know what requirements the formatting for the xml are for one) but I'll give it a go! 但这仍然充满问题(因为我不知道xml的格式要求是什么),但我会努力的! So just tell me if you need to use your config.php or not, then if no, I'll quickly code something. 因此,只要告诉我是否需要使用config.php,那么如果不需要,我会快速编写一些代码。 Otherwise you're going to have to post the contents of config.php 否则,您将不得不发布config.php的内容

Cheers 干杯

Ok here's my best guess at what would work: 好的,这是我能做的最好的猜测:

<?xml version="1.0" encoding="utf-8"?>
<playlist version="1" xmlns="http://xspf.org/ns/0/">
    <trackList>
<?php

$DB_SERVER = "localhost";
$DB_NAME = "yourdatabasename";
$DB_USER = "yourdbuser";
$DB_PASSWORD = "yourpw";

mysql_connect($DB_SERVER, $DB_USER, $DB_PASSWORD);
mysql_select_db($DB_NAME);

$sql = "select * from songlist order by date_added desc limit 10";

$res = mysql_query($sql);

while ($result = mysql_fetch_assoc($res)){
    ?>
        <track>
            <Song><![CDATA[<?= $result['title']?]]></Song>
            <album><![CDATA[<?= $result['artist']?>]]></album>
        </track>
    <?
}
?>
</trackList>
</playlist>     

Just change your database name, user and password, then you will have your xml. 只需更改数据库名称,用户和密码,即可获得xml。 However, you might want to change your album line above to: 但是,您可能希望将上面的相册行更改为:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM