开发者

Creating webservice(XML) with php for java

开发者 https://www.devze.com 2023-03-19 06:38 出处:网络
Am trying to write a php script that exports a database and prints out the contents as XMl. So for i have this

Am trying to write a php script that exports a database and prints out the contents as XMl. So for i have this

<?php
require('connect.php');

$query = mysql_query("SELECT * FROM blog_comments");
$database="MyWebsite";
$table = "blog_comments";
echo mysql_error();


echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";

echo "<$database>";
$i=0;
while($row=mysql_fetch_assoc($query))
{
    echo "<$table>";
            while ($i < mysql_num_fields($query))
            {
                $meta = mysql_fetch_field($query);
                echo "<".$meta->name.">".$row['$meta->name']."</".$meta->name."><br/>";
                $i++;
            }
            $i=0;
    echo "</$table>";
}

echo "</$database>";


?>

And my output is

<?xml version="1.0" encoding="utf-8" ?>
<MyWebsite>
<blog_comments>
    <></>
    <></>
    <></>
    <></>
    <></>
    <></>
    <></>
    <></>
</blog_comments>
<blog_comments>
    <></><></><></><></><></><></><></><></>
</blog_comments>
<blog_comments> 
    <></><></><></><></><></><></><></><></>
</blog_comments>
<blog_comments>
    <></><></><></><></><></><></><></><></>
</blog_comments>
<blog_comments>
    <></><></><></><></><></><></><></><></>
</blog_comments>
<blog_comments>
    <></><></><></><>开发者_开发百科</><></><></><></><></>
</blog_comments>
<blog_comments>
    <></><></><></><></><></><></><></><></>
</blog_comments>
<MyWebsite>

The number of everything is correct but for some reason i am not getting the values printed. Am doing this because my websites host doesn't allow remote database connections and I need to connect to a database from my java application. So i want to call a php page from my java app and it would respond with xml containing my data and i then parse this XML and use the relevant data

I need help on how to make this work so i can achieve the above goal.. thanks Thanks a lot.


I'm not positive, but I think mysql_fetch_assoc is basically "burning" your query result, and that for the same query, you can't call it first. However, you don't need to grab all this field information if all you want is the name, just use:

echo "<{$table}>";
foreach($row as $key => $val){
    echo "<{$key}>{$val}</{$key}>";
}
echo "</{$table}>";
0

精彩评论

暂无评论...
验证码 换一张
取 消