1

In this program I'm trying to take a serialized array out of a MySQL table and then adding one result to it, and then re-serializing that array and putting it back into the table. Here is my code:

for($i = 0; $i < $count; $i++){
        $query = "SELECT `assignment_assigned` FROM `users` WHERE isAdmin = 0 LIMIT 1";
        $result = mysqli_query($con, $query);
        if($result === FALSE) { 
            echo "Failed".mysql_error();
        } else {
            $unserializedVal = unserialize(implode(mysqli_fetch_array($result)));
            $array = array_push($unserializedVal, $val);
            $serializedVal = serialize($unserializedVal);
}
<h1><?php print_r($unserializedVal); ?></h1> 

When I do this, I retrieve the array and can unserialize it just fine, but when I try to reserialize it, this shows up:

a:8:{i:0;i:2;i:1;i:3;i:2;i:4;i:3;i:5;i:4;i:6;i:5;i:7;i:6;i:8;i:7;s:2:"10";}

The original serialized string was this:

a:7:{i:0;i:2;i:1;i:3;i:2;i:4;i:3;i:5;i:4;i:6;i:5;i:7;i:6;i:8;}

The array before I append the last numeral looks like this:

Array ( [0] => 2 [1] => 3 [2] => 4 [3] => 5 [4] => 6 [5] => 7 [6] => 8 )

And after appending the last number:

Array ( [0] => 2 [1] => 3 [2] => 4 [3] => 5 [4] => 6 [5] => 7 [6] => 8 [7] => 10 )

Any help here?

sahmed
  • 35
  • 8
  • Quick hint: since you always run the same query (not dynamic), just save the result in a variable before the `for`. That way you won't be running multiple selects that supposedly return the same result every time... – FirstOne Jul 03 '16 at 22:41
  • I just realized, you are running `mysqli_*` functions, but when it comes to showing errors, you use `mysql_error`. Note that you [**can't mix APIs**](http://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php) – FirstOne Jul 03 '16 at 23:01

2 Answers2

1

You're getting s:2:"10"; added to your serialized array because the value you added is a string, not an integer.

You could convert the string to an integer using intval().

$val = intval($val);

For error checking intval() will return 0.

Edited to fix error that the db data will array.

$originalSerializedVal = mysqli_fetch_array($result);
$unserializedVal = unserialize($originalSerializedVal['0']);
$val = intval($val);
// add error handling here
$array = array_push($unserializedVal, $val);
$serializedVal = serialize($unserializedVal);

Note: removed implode() as pointed out by others. The data from mysqli_fetch_array is an array, but you are only interested in the selected field. implode will convert the array to a string, but is unnecessary.

Tristan
  • 3,301
  • 8
  • 22
  • 27
  • It seems that the problem was the `$val` not being an integer, but if i take out `implode`, `array_push` doesn't work, so I do in fact need `implode()`. – sahmed Jul 03 '16 at 23:11
0

implode() does not make a serialized string, so you cannot do:

$unserializedVal = unserialize(implode(mysqli_fetch_array($result)));

simply use:

$unserializedVal = mysqli_fetch_array($result);

I cannot garantee that your code will now do what you intend it to do, but this was a clear error.

KIKO Software
  • 15,283
  • 3
  • 18
  • 33
  • The problem is if i delete implode then the system will throw me this error: Warning: unserialize() expects parameter 1 to be string, array given – sahmed Jul 03 '16 at 23:02
  • That's because you followed Tristan's advice, and he's got it wrong. Please read by answer carefully. – KIKO Software Jul 03 '16 at 23:06