3

the following error is thrown:

Fatal error: Trying to clone an uncloneable object of class mysqli_result

when I'm trying to use clone directly for a mysql query result:

$result = mysqli_query($con, $query);
$resultsClone = clone $result;

is there a way to clone mysqli object?

hakre
  • 193,403
  • 52
  • 435
  • 836
3emad
  • 238
  • 3
  • 10
  • In extreme cases, there is this abomination: `$clone = unserialize(serilize($target));` We are assuming that `$target` can be serialized, which is something else. – Christian Apr 25 '12 at 19:11

4 Answers4

3

Cloning an unclonable object is not possible, as the name suggests.

Just because to tell a little bit more: In this specific case a mysqli-result is (more or less -- I don't know the exact details) a pointer to a result "somewhere else". Cloning would mean, that two pointers referes to the same result "somewhere else", what again will probably lead to severe side effects, because fetching results from one result will definitely affect the other one.

KingCrunch
  • 128,817
  • 21
  • 151
  • 173
3

As KingCrunch already explained, there is not much use in cloning database result pointers - they have a limited context in which they live - as any pointer or resource.

Depending on what you want to do however, you can just run the query a second time:

$result      = mysqli_query($con, $query);
$resultClone = mysqli_query($con, $query);

Or you encapsulate the fetch logic so that you rewind on the same resource depending on the state of your application.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • Doing that would be expensive. What i did as work around by looping the 1st time extract the data i want to reuse it in the 2nd loop output and reset the pointer using data_seek – 3emad Apr 26 '12 at 17:55
  • @3emad: That's the more encapsulated thing I meant. – hakre Apr 26 '12 at 18:42
0

I faced a similar issue when trying to use the output of MySQL results as a dictionary to get titles based on id.

As mysqli_result acts as a pointer to mysqli_query output stored somewhere in the server, means that once it's fetched it's no longer found it returns NULL when trying to access it again (multiple iterations or another). One way I was able to overcome it was by fetching the mysqli_result to a JSON array which allowed for it to act as a dictionary (iterate through it infinite time).

$result = mysqli_query($con, $query);
while($row = mysqli_fetch_assoc($result )) {
    $results_clone[] = $row ; 
}

Now you shall be able to use/access $results_clone infinite times. Sample code:

for($i=0;$i<count($results_clone);$i++){
    $results_clone[$i];
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Osama Hussein
  • 69
  • 2
  • 8
  • What does this have to do with cloning objects? – Dharman Mar 09 '23 at 11:30
  • 1
    The question was about cloning/copying a mysqli_result to be used. My answer was a workaround that delivers similar output especially since mysqli_result can't be cloned and once fetched it will return null. – Osama Hussein Mar 09 '23 at 12:08
-1

Move the pointer (return) to the zero line of the result. http://www.php.net/manual/ru/mysqli-result.data-seek.php

Alex
  • 7
  • 2