我的问题是,当我尝试获取数据时,我的数组上会出现空指针。
当我在phpmyadmin中运行我的sql命令时,我得到了很好的结果。
我还使用echo
检查$ids[$i]
是否为空。
错误消息:
Warning: array_push() expects parameter 1 to be array, null given
感谢您的帮助。
这是我的代码:
while($i < $size)
{
$mysqli = new mysqli(****);
$sql = "SELECT workout_name, user, likes, dislikes, date FROM workouts_wall WHERE id_for_wall = ? ";
$stmt = $mysqli->prepare($sql) or trigger_error($mysqli->error."[$sql]");
$stmt->bind_param('i', $ids[$i]);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($workout_name, $user, $likes, $dislikes, $date);
if($stmt->fetch())
{
// temp user array
$workouts = array();
$workouts["workout_name"] = $workout_name;
$workouts["picture"] = getPicture($user);
$workouts["user"] = $user;
$workouts["likes"] = $likes;
$workouts["dislikes"] = $dislikes;
$workouts["date"] = $date;
// push single product into final response array
array_push($response["workouts"], $workouts);
}
$i++;
}
答案 0 :(得分:3)
您的变量$response["workouts"]
本身未实例化为数组,因此array_push
无法将其识别为数组。尝试在while
循环之前添加此内容:
$response["workouts"] = array();
由于您一次只向数组添加一个元素,因此使用以下代码(而不是array_push
调用)会更简单,更简单:
$response["workouts"][] = $workouts;