Changinng functions to mysqli

Hi All,

It’s been a while since I have been on sitepoint.

I have just upgraded the PHP version and was get deprecated errors…so it looks like I need to use mysqli.

My site is written using functions, so I am trying to write them using mysqli.

function list_categories($conn) {
	$query = "SELECT category_id FROM category ORDER BY category_name_en";
	$result = $conn->query($query);
	while ($array = mysqli_fetch_assoc($result)) {
		array_push($list_categories, $array['category_id']);
	return $list_categories;

However this is producing an error:

Missing argument 1 for list_categories()

But I am passing $conn to the function.

Any ideas??

How have you defined $conn?

Just before the while loop add:


That will set up $list_categories as a new (empty) array before the while loop adds stuff to the array

1 Like

I thought the error message was referring to the function list_categories($conn), not the variable $list_categories.

There is a note in the doc that says the array must exist before you call array_push() and that it will issue a warning if it is not. "Note: array_push() will raise a warning if the first argument is not an array. This differs from the $var behaviour where a new array is created. "

Note, with PDO instead of myqli this would be more straightforward:

function list_categories(PDO $conn) {
	$query = "SELECT category_id FROM category ORDER BY category_name_en";
	$result = $conn->query($query);
	$list = $result->fetchAll(PDO::FETCH_COLUMN);
	return $list;

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.