views:

136

answers:

4

In my PHP file,I'm reading out bulk of information using query like below:

SELECT  GROUP_CONCAT(CONCAT('<comment><body><![CDATA[',body,']]></body>','<replier>',if(screen_name is not null and !anonymous,screen_name,''),'</replier>','<created>',created,'</created></comment>') SEPARATOR '')
         FROM   idiscussion
         LEFT JOIN
                users
         ON     idiscussion.uid=users.id
         WHERE  idiscussion.iask_id = 1

yes,it's like xml format,but seems it's not easiest to parse in PHP,

Any recommendation on which format to use in order to parse easily later on?

AS for my I need to concatenating them all together,

You can think about this case: To retrieve 1 topic and discussions under this topic at a time. 1 topic corresponds to multiple discussions.How can you do it in 1 query without concatenating it together?

A: 

Depends on the data structure, but a list of values may easily be saved as a semi-colon separated list of entries using join(), and then easily parsed using split().

This is extremely simple, but may not be ideal for slightly more complex data.

Answer to comment: if the data contains a semi-colon, it needs to be escaped as appropriate.

stefpet
What if the data contains a semi-colon?
elcuco
yes,I think your approach is nice,but seems the delimiter is a key point.
Shore
A: 

You could use DOM or SimpleXML to parse XML files, but also JSON or YAML. All are quite easy to use as you can rely on high-level parsers for PHP. They have advantages and disadvantages depending on how you want to use the format.

okoman
So you hint that xml is the easiest format to parse in PHP?
Shore
+2  A: 

If you're getting data from a SQL database, it's much easier not to use SQL to clobber it into some text format, but to instead perform a regular query and use the SQL functions to work with the results that you're given.

If you end up needing to serialize data to other PHP clients, I would recommend PHP's standard serialization format.

Paul Fisher
I'm clobbering it into some text format to get all information in 1 query,you are right,but if not so,will need to query several time,at least the round trip time is wasted.
Shore
If this single query is able to retrieve all the data to build the string... what makes you think it can't just pass that same data back to php? Assuming you can't write a simple JOIN to get all your data in a single query, there are still other options much better than creating (and later parsing) an XML string. You could create a stored procedure, a view, or build a temporary table.
Frank Farmer
A: 

Can I ask why you're not just retrieving the data without concatenating it all together?

SELECT body,
       IF(screen_name IS NOT NULL AND !anonymous, screen_name, '') AS replier,
       created
FROM   idiscussion
LEFT JOIN
       users
ON     idiscussion.uid = users.id
WHERE  idiscussion.iask_id = 1

This should execute as quickly as your original query, and you can access its data as you would any other result set. Assuming you're using MySQL and not using PDO:

$result = mysql_query($sql);
if ($result) {
    while ($row = mysql_fetch_assoc($result)) {
        // $row is an associative array containing the current row's data.
    }
}

If you need to create the XML but also want to access the data in its raw form, just create it in the PHP.

$xml =
    '<comment>' .
         '<body><![CDATA[' . $row['body'] . ']]></body>' .
         '<replier>' . $row['replier'] . '</replier>' .
         '<created>' . $row['created'] . '</created>' .
    '</comment>';

You may need to escape the data if it hasn't been sanitised already. You could also use an XML builder such as XMLWriter, though that's beyond the scope of this question.

Samir Talwar
You can think about this case:To retrieve 1 topic and discussions under this topic at a time.1 topic corresponds to multiple discussions.How can you do it in 1 query without concatenating it together?
Shore
Multiple rows, perhaps? Of the form: topic_id, discussion_id, discussion_name, discussion_body, discussion_replier, discussion_created. The topic ID will always be the same across all rows, but the rest won't.
Samir Talwar