0
<?
    $MySQLPassword = "indian";  
    $HostName = "localhost";    
    $UserName = "monty";

    mysql_connect($HostName,$UserName,$MySQLPassword)
    or die("ERROR: Could not connect to database!");

    mysql_select_db("sachin") or die("cannot select db");

    $keyword = $_POST['keyword'];
    echo $keyword;
   /* Execute the query that performs the actual search in the DB: */
   $result = mysql_query(" SELECT p.page_url AS url,
                           COUNT(*) AS occurrences 
                           FROM page p, word w, occurrence o
                           WHERE p.page_id = o.page_id AND
                           w.word_id = o.word_id AND
                           w.word_word = \"$keyword\"
                           GROUP BY p.page_id
                           ORDER BY occurrences DESC
                           " );

$output = new DOMDocument();
$output->formatOutput = true;
$output = "<loginsuccess>";

for( $i = 1; $row = mysql_fetch_array($result); $i++ )      {


$output .="<person><keyword>".$_POST['keyword']."</keyword><name>".$row['url']."</name><occur>".$row['occurrences']."</occur></person>";
}

$output .= "</loginsuccess>";
print ($output);

?>

I am getting the output as XML, but i need to store it in a separate xml file, can any one help me out.

One more question,....

I am using fwrite and able to write into a file, but how can i clear the file each time it writes, instead of writing at the end of file? i need the existing content to be destroyed and re written again.

1
  • Note that you might get trouble by outputting raw data from the DB to XML without escaping special characters. Particularly the URL where you can have & chars. Commented May 25, 2009 at 12:13

4 Answers 4

4

Use fwrite.

EDIT: To truncate, use:

$handle = fopen($filename , "wb")

when doing the open. The w means open for writing and truncate to 0 length. The b means binary mode so Windows doesn't mess with your line endings.

Sign up to request clarification or add additional context in comments.

1 Comment

addition: you can use xml_encode() to save some lines in your code.
2

file_put_contents is the easiest way of dumping a string to file.

Comments

0

Just the simple file functions, check them out over at the PHP.net website.

Comments

-2

In addition, you wanna make sure your properly escape the contents of the XML file. htmlentities() is a good place to start reading up on the subject, or you can just use <![CDATA[ ... ]]>

Comments

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.