I have two php functions, one returns a JSON array and the other attempts to decode it and access it's contents in order to strip a URL out of it. My problem is that I get the following errors..
1) json_decode() expects parameter 1 to be string
2) Invalid argument supplied for foreach() on line 77
Here is my code
php file 1
<?php
function getAnimation($userid, $db) {
include('connect.php');
$db = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error() . "please contact [email protected] for technical assistance";
echo "<br>";
}
$box_num = 1;
$select = "SELECT card_id, order_num FROM decks WHERE box_num=$box_num AND id=$userid ORDER BY order_num";
$result = mysqli_query($db, $select) or die("SQL Error 1: " . mysqli_error($db));
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) {
$users[] = array(
'card_id' => $row['card_id'],
'order_num' => $row['order_num'],
);
}
json_encode($users);
return $users;
mysqli_close($db);
}
?>
snippet of php file 2 that is attempting to decode the JSON array and access its contents.
include('getAnimation.php');
$animation = getAnimation($result_array[0], $db); //<< atempting to access returned array here
$obj = json_decode($animation); //FIRST ERROR HERE
foreach ($obj->card_id as $item) { // SECOND ERROR HERE
$url = ($item->card_id);
echo $url; //show me the money
}
thanks for any help you can give!