Okay so I need to push the array that is fetched from a few different tables into a new array. I thought this would do it but it doesn’t. A warning saying array_push() expects parameter 1 to be an array. There is probably something really simple that I have done wrong but Im new to all this PHP stuff so have no idea.
I thought the parameter 1 is an array as an array is fetched from database.
Here’s the code:
$newsfeed = array("apple");
$news = mysql_query("
SELECT * FROM news
UNION ALL
SELECT * FROM feature ORDER BY timestamp DESC LIMIT 1
")or die(mysql_error());
while($row = mysql_fetch_array($news))
{
$artist = mysql_query("
SELECT * FROM members WHERE artist='Y'
ORDER BY timestamp DESC LIMIT 2
")or die(mysql_error());
while($row1 = mysql_fetch_array($artist))
{
array_push($newsfeed, $row['title'], $row1['artistname']);
}
}
echo($newsfeed);
Look at the documentation for array_push. The first parameter must be the name of the array you’re pushing into.
$myArraywill now have$row['title']and$row1['artistname']in it.You’re also missing braces on the while loop. You can only omit braces when there is only one line following it. It’s still good practice to have braces anyway.
If you want to see the value of an array, you should use
print_rinstead ofecho.If you want to print out the elements of the array, you’ll need to loop through them. Then you can use
echo, when you’re only printing one element of the array. You can’t use it for an entire array or it’ll just printarray. See theforeachdocumentation for examples on how to do this: