开发者

Best format in php to save mysql result to a file?

开发者 https://www.devze.com 2023-01-10 02:03 出处:网络
I\'m setting up a caching system in PHP for database queries. So when a query like \"SELECT * FROM table\" is called, it either returns the cached results of that query or the results directly from t

I'm setting up a caching system in PHP for database queries.

So when a query like "SELECT * FROM table" is called, it either returns the cached results of that query or the results directly from the DB.

$query = "SELECT * FROM table";
[...]
$data = mysql_query($query);
[...]
fwrite($file,json_encode($data));
[...]

The problem is I'm trying to save the query results to a file and I can't find a textual format that works. I've tried json_encode and serialize, and they both re开发者_Python百科turn null or 0. Is there a format that will work for this without having to do mysql_fetch_array() and then serialize?

This is the error I get with json, obviously because I haven't converted the result to an array:

Warning: [json] (php_json_encode) type is unsupported, encoded as null


why not:

  1. use native mysql cache?
  2. optimize queries so they work fast and don't need to be cached?


It may not be a perfect solution for the issue you are handling, but I have found Justin Vincent's ezSQL Class to be really handy for working with situations like this. The class includes a caching function, which would satisfy the needs you mentioned in the question above. (Bonuses being that it handles alot of the more obtuse PHP<=>MySQL interactions as well.)

0

精彩评论

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