views:

34

answers:

1

I have the following scenario. I must run a query and then save it to memory, and if I need the same result again to obtain it from memory.

Storing and reading from memory is made with memcache. The problem is that if you hold information in an array one at a time just have to treat two cases: 1 for resource type and one for array type.

The question is if I can convert an array to Mysql resource type.

Thanks.

A: 

do something like this

<?php

$db = mysql_connect("localhost","foo_dbo","pass") or die("Database error");

mysql_select_db("foo_db", $db);

$sql = "select * from posts;";

$file = sprintf("%s.dat", md5($sql));

$result = mysql_query($sql);

if (!$result) die("Invalid query: " . mysql_error());

while ($row = mysql_fetch_assoc($result)) $data[]=$row;

mysql_free_result($result);
mysql_close($db);

if($fp=fopen($file,"w")){
    fwrite($fp,serialize($data));
    fclose($fp);
}

?>

then to read back into an array do something like

unserialize(file_get_contents($file))

obviously you'd use memcache to store the serialised data not the file system

hope this helps

f00