Asked  1 Year ago    Answers:  5   Viewed   31 times

i have a small problem with my mysqli related function. here is the code:

$query = "select * from uploads";
 if ($result = $mysqli->query($query)) {
   while ($row = $result->fetch_assoc()) {
     return $row;
  }

the problem is that when i use this function $row appears to be an array, but with only the first value from the query result in it. but if i try to return var_dump($row) instead, function displays array as expected, with all the values from the query result. can you please explain why is this happening and how to return an array with complete query result correctly. thanks!

 Answers

2
$data  = array();
$query = "select * from uploads";
if ($result = $mysqli->query($query)) {
  while ($row = $result->fetch_assoc()) {
     $data[] = $row;
  }
}
return $data;
Thursday, April 1, 2021
 
Jubair
 
1

just replace it with mysqli_fetch_array or mysqli_result::fetch_array :)

while( $row = $result->fetch_array() )
{
    echo $row['firstname'] . " " . $row['lastname'];
    echo "<br />";
}

almost all mysql_* functions have a corresponding mysqli_* function.

Thursday, April 1, 2021
 
muaaz
 
2

when i assign the results of the second query to $results, what happens to the memory associated with the previous results?

when you execute this:

$results = $db->query($query);

if there was something in $results before, this old content cannot be accessed anymore, as there is no reference left to it.

in such a case, php will mark the old content of the variable as "not needed anymore" -- and it will be removed from memory when php needs some memory.

this, at least, is true for general php variables; in the case of results from an sql query, though, some data may be kept in memory on the driver-level -- over which php doesn't have much control.


should i be freeing that result before assigning the new one?

i never do that -- but, quoting the manual page of mysqli_result::free:

note: you should always free your result with mysqli_free_result(), when your result object is not needed anymore

it probably doesn't matter for a small script... and the only way to be sure would be to test, using memory_get_usage before and after calling that method, to see whether there is a difference or not.


related to 1, when i do clean up at the end, is cleaning up just the last results enough?

when the scripts end:

  • the connection to the database will be closed -- which means any memory that might be used by the driver should be freed
  • all variables used by the php script will be destroyed -- which means the memory they were using should be freed.

so, at the end of the script, there is probably really no need to free the resultset.


when i do try to clean up a result, should i be freeing it as above, should i be closing it, or both?

if you close the connection to the database (using mysqli::close like you proposed), this will disconnect you from the database.

which means you'll have to re-connect if you want to do another query! which is not good at all (takes some time, resources, ... )

generally speaking, i would not close the connection to the database until i am really sure that i won't need it anymore -- which means i would not disconnect before the end of the script.

and as "end of the script" means "the connection will be closed" even if you don't specify it; i almost never close the connection myself.

Thursday, April 1, 2021
 
danjah
 
3

i'll need to do some tests to ensure that there's no adverse effects in other situations; but the following looks as though it might fix this problem, and certainly calculates the correct rate value for your arguments rate(60, -6000, 120000) stabilises at 0.046781916422493 in iteration 15.

define('financial_max_iterations', 128); 
define('financial_precision', 1.0e-08); 


function rate($nper, $pmt, $pv, $fv = 0.0, $type = 0, $guess = 0.1) { 

    $rate = $guess; 
    if (abs($rate) < financial_precision) { 
        $y = $pv * (1 + $nper * $rate) + $pmt * (1 + $rate * $type) * $nper + $fv; 
    } else { 
        $f = exp($nper * log(1 + $rate)); 
        $y = $pv * $f + $pmt * (1 / $rate + $type) * ($f - 1) + $fv; 
    } 
    $y0 = $pv + $pmt * $nper + $fv; 
    $y1 = $pv * $f + $pmt * (1 / $rate + $type) * ($f - 1) + $fv; 

    // find root by secant method 
    $i  = $x0 = 0.0; 
    $x1 = $rate; 
    while ((abs($y0 - $y1) > financial_precision) && ($i < financial_max_iterations)) { 
        $rate = ($y1 * $x0 - $y0 * $x1) / ($y1 - $y0); 
        $x0 = $x1; 
        $x1 = $rate;
        if (($nper * abs($pmt)) > ($pv - $fv))
            $x1 = abs($x1);

        if (abs($rate) < financial_precision) { 
            $y = $pv * (1 + $nper * $rate) + $pmt * (1 + $rate * $type) * $nper + $fv; 
        } else { 
            $f = exp($nper * log(1 + $rate)); 
            $y = $pv * $f + $pmt * (1 / $rate + $type) * ($f - 1) + $fv; 
        } 

        $y0 = $y1; 
        $y1 = $y; 
        ++$i; 
    } 
    return $rate; 
}   //  function rate() 
Saturday, May 29, 2021
 
Crontab
 
4

this works:

function sub_array(array $haystack, array $needle)
{
    return array_intersect_key($haystack, array_flip($needle));
}

$myarray = array(
    'apple'=>'red',
    'banana'=>'yellow',
    'lettuce'=>'green',
    'strawberry'=>'red',
    'tomato'=>'red'
);
$keys = array(
    'lettuce',
    'tomato'
);

$ret = sub_array($myarray, $keys);

var_dump($ret);
Friday, August 13, 2021
 
Enlico
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :