Not sure if this belongs in here or databse anyway i have a script were i want to save the result of mysql_query into a var so i can it later on.
this is the script:
<?
mysql_connect("*","*","*");
mysql_select_db("*");
$memory = array();
function do_query($query){
global $memory;
if($memory[$query]){
echo "<i>Query found in memory</i><br>";
return $memory[$query];
}else{
echo "<i>Query NOT found in memory</i><br>";
$result = mysql_query($query);
$memory[$query] = $result;
return $result;
}
}
function microtime_diff($a, $b) {
list($a_dec, $a_sec) = explode(" ", $a);
list($b_dec, $b_sec) = explode(" ", $b);
return $b_sec - $a_sec + $b_dec - $a_dec;
}
///
$start_time = microtime();
$row = mysql_fetch_array(do_query("SELECT * FROM ic_users"));
echo $row['username']."<br>";
$duration = microtime_diff($start_time, microtime());
$duration = sprintf("%0.3f", $duration);
echo ("Processing took $duration seconds");
///
echo "<hr>";
///
$start_time = microtime();
$row = mysql_fetch_array(do_query("SELECT * FROM ic_users"));
echo $row['username']."<br>";
$duration = microtime_diff($start_time, microtime());
$duration = sprintf("%0.3f", $duration);
echo ("Processing took $duration seconds");
///
?>
and the result is:
Query NOT found in memory
taco
Processing took 0.049 seconds
--------------------------------------------------------------------------------
Query found in memory
Processing took 0.000 seconds
i also tried adding OR die(mysql_error());
it did die at the second, but mysql_error() was empty.
how can i fix this
thanks in advanceI am not sure what you want?
if iti s not in memory you want to run mysql_error? that won't show anything as it is not an error.it DOES is in the $memory error, look at the output it shoes Query found in memory .
i just want to see what it matters in time
-i call do_query function with a query, the query isn't in the $memory array yet so mysql_query is called
-than i call do_query for the second time, it again checks if that query has been used before and is in $memory, if it is don't do mysql_query but return the value of the $memory array key.
i know it's not empty i echoed out the $memory array and it was like:
Array
(
[SELECT * FROM ic_users] => Resource id #2
)
so it's in the array and the do_query() function does return it when it's called the second time. at the second call mysql_fetch_array(do_query(..)) just can't use the resource id returned by do_query() you are returning the wrong info if it is in the array.
$row = mysql_fetch_array("SELECT * FROM ic_users");
that is what it returns now.
so either return this
return $memory[$query][0];
or add it like this
$memory[] = $result;
but you need a way to search the array before you return it.
I see what you are doing and it will only work is if you know what key is holding the resource id# you want. all you are doing is retuning the key, not the index.
you may have to use a for loop to loop through the array and see which one matches. that is if you have more than one key in that array.it actually does return teh resource id and not the query.
and im pretty sure
return $memory[$query];
returns the value of that key, and not the key itself. or maybe i just misunderstood you.
anyway i found out the problem, the script was ok, i just found out that table only contains 1 row } therefore the second call was empty. mysql_data_seek($result,0); fixed that.
thanks for your time anyway :rocker:
this is the script:
<?
mysql_connect("*","*","*");
mysql_select_db("*");
$memory = array();
function do_query($query){
global $memory;
if($memory[$query]){
echo "<i>Query found in memory</i><br>";
return $memory[$query];
}else{
echo "<i>Query NOT found in memory</i><br>";
$result = mysql_query($query);
$memory[$query] = $result;
return $result;
}
}
function microtime_diff($a, $b) {
list($a_dec, $a_sec) = explode(" ", $a);
list($b_dec, $b_sec) = explode(" ", $b);
return $b_sec - $a_sec + $b_dec - $a_dec;
}
///
$start_time = microtime();
$row = mysql_fetch_array(do_query("SELECT * FROM ic_users"));
echo $row['username']."<br>";
$duration = microtime_diff($start_time, microtime());
$duration = sprintf("%0.3f", $duration);
echo ("Processing took $duration seconds");
///
echo "<hr>";
///
$start_time = microtime();
$row = mysql_fetch_array(do_query("SELECT * FROM ic_users"));
echo $row['username']."<br>";
$duration = microtime_diff($start_time, microtime());
$duration = sprintf("%0.3f", $duration);
echo ("Processing took $duration seconds");
///
?>
and the result is:
Query NOT found in memory
taco
Processing took 0.049 seconds
--------------------------------------------------------------------------------
Query found in memory
Processing took 0.000 seconds
i also tried adding OR die(mysql_error());
it did die at the second, but mysql_error() was empty.
how can i fix this
thanks in advanceI am not sure what you want?
if iti s not in memory you want to run mysql_error? that won't show anything as it is not an error.it DOES is in the $memory error, look at the output it shoes Query found in memory .
i just want to see what it matters in time
-i call do_query function with a query, the query isn't in the $memory array yet so mysql_query is called
-than i call do_query for the second time, it again checks if that query has been used before and is in $memory, if it is don't do mysql_query but return the value of the $memory array key.
i know it's not empty i echoed out the $memory array and it was like:
Array
(
[SELECT * FROM ic_users] => Resource id #2
)
so it's in the array and the do_query() function does return it when it's called the second time. at the second call mysql_fetch_array(do_query(..)) just can't use the resource id returned by do_query() you are returning the wrong info if it is in the array.
$row = mysql_fetch_array("SELECT * FROM ic_users");
that is what it returns now.
so either return this
return $memory[$query][0];
or add it like this
$memory[] = $result;
but you need a way to search the array before you return it.
I see what you are doing and it will only work is if you know what key is holding the resource id# you want. all you are doing is retuning the key, not the index.
you may have to use a for loop to loop through the array and see which one matches. that is if you have more than one key in that array.it actually does return teh resource id and not the query.
and im pretty sure
return $memory[$query];
returns the value of that key, and not the key itself. or maybe i just misunderstood you.
anyway i found out the problem, the script was ok, i just found out that table only contains 1 row } therefore the second call was empty. mysql_data_seek($result,0); fixed that.
thanks for your time anyway :rocker: