-1

I define my $link in database.php:

$link=mysqli_connect("localhost","root","","oop");

if (mysqli_connect_errno($link))
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

After that i include this in my header.php:

include "../includes/database.php";

Then i use it in the mysqli_query calls:

$result = mysqli_query($link, "SELECT * FROM Menu") 
          or die(mysql_error());
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    echo '<div id= menulist>';
    echo '<div class="menu" id="menu-'.$row['menu_id'].'">';
    echo $row['menu_name'] . " - ". $row['menu_weight']
         . "<a class='delete' href='?delete=".$row['menu_id']
         ."'><img src='images/delete.png' /></a>";
    echo '</div>';

The following error occurs:

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /Applications/XAMPP/xamppfiles/htdocs/www/oop/admin/menu_class.php on line 39

What did i do wrong?

Dharman
  • 30,962
  • 25
  • 85
  • 135
user2237168
  • 305
  • 1
  • 3
  • 17
  • Possible duplicate of [Should I pass my $mysqli variable to each function?](http://stackoverflow.com/questions/14016462/should-i-pass-my-mysqli-variable-to-each-function) – John_West Jun 16 '16 at 22:47
  • Does this answer your question? [Warning: mysqli\_query() expects parameter 1 to be mysqli, null given in](https://stackoverflow.com/questions/18862743/warning-mysqli-query-expects-parameter-1-to-be-mysqli-null-given-in) – Dharman Jan 04 '20 at 20:35

1 Answers1

1

Your problem has nothing to do with mysqli. It's Variable scope issue. Use global keyword to make $link available. And it's obviously a duplicate question of many

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345