0

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.

6
  • The number of everything is correct but for some reason i am not getting the values printed. I need to make it work so that i can use the database from my java applicaion. Commented Jul 9, 2011 at 18:56
  • Next time please include your code and output in your question. This way, potential answerers don't need to navigate to a different site to understand your question. I've taken the liberty of including the code and output here. Commented Jul 9, 2011 at 19:02
  • I tried to but it wasn't fitting properly, how did you do it? Commented Jul 9, 2011 at 19:05
  • I pasted your code into the box and made sure before every line of code there were 4 spaces and that before the entire block there was an empty line and that after the entire block there was an empty line. This amounted to indenting the first line by 4 spaces. You can always just hit the {} button. See the Editing Help for details. Commented Jul 9, 2011 at 19:09
  • Why don't you connect directly to mysql server via JDBC (java database connectivity)? Commented Jul 9, 2011 at 19:09

1 Answer 1

1

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}>";
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot man. I really do appreciate...Was really helpful
@DaMainBoss: On StackOverflow it's encouraged you accept the best answer for your question by clicking the checkmark next to that answer. If this solves your problem and you receive no better responses, please consider accepting it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.