omg
omg

Reputation: 139872

what's the most easy-to-parse format for PHP?

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?

Upvotes: 0

Views: 278

Answers (4)

Samir Talwar
Samir Talwar

Reputation: 14330

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.

Upvotes: 0

Paul Fisher
Paul Fisher

Reputation: 9666

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.

Upvotes: 2

okoman
okoman

Reputation: 5657

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.

Upvotes: 0

stpe
stpe

Reputation: 3628

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.

Upvotes: 0

Related Questions