Function Trouble

I’m having trouble with a function I’m trying to have a function where it displays a notification for the user whenever a new update is posted but whenever I call the function I get this error " Fatal error : Uncaught Error: Call to undefined method mysqli_stmt::fetch_array() in /home/evoarena/public_html/Dev/functions.php:67 Stack trace: #0 /home/evoarena/public_html/Dev/header.php(140): notifications(Object(mysqli)) #1 /home/evoarena/public_html/Dev/login.php(3): require(’/home/evoarena/…’) #2 {main} thrown in /home/evoarena/public_html/Dev/functions.php on line 67"

What am I doing wrong>
Line 67 is

$news_user = $news_stmt->fetch_array();

Function Code:

function notifications($link) {
 if (isset($_SESSION['username'])) {
  $username = $_SESSION['username'];
    $news_query = "SELECT news_read FROM users where username=?";
    if ($news_stmt = $link->prepare($news_query)) {    
      $news_stmt->bind_param("s", $username);
      $news_stmt->execute();
      $news_result = $news_stmt->get_result();
      $news_user = $news_stmt->fetch_array();      
      if ($news_user['news_read'] == '0') {      
          echo "<div class='alert alert-primary alert-dismissible' role='alert'>
        <center><button type='button' class='close' data-dismiss='alert'>&times;</button>There is a new post on the <a href='http://www.pereia.net/Dev/news/index.php'>Updates</a> page
        </center></div>";      
      }      
    } else {    
       $error = $link->errno . ' ' . $link->error;
       echo $error;
    }
  }
}

The following link is to the mysqli_stmt documentation - https://www.php.net/mysqli_stmt

Yep. The error message is correct. There is no mysqli_stmt method named fetch_array().

It does have a fetch() method, requiring that you use the bind_result() method first and it has a get_result() method, which only exists in a specific server configuration and shouldn’t be used unless you manage your own servers.

Since you are using the get_result() method, you end up with a mysqli_result object in $news_result, which does have a fetch_array() method. Perhaps proof-read your code to make sure of what variable is being used.

If you switch to the much simpler and more consistent PDO extension, several of those lines of code will go away. You will only have a prepare(), execute(), and fetch() statement.

I have used fetch_array in other queries and it has never given me an error before

I think you missed something in phdr’s comment.

Sponsor our Newsletter | Privacy Policy | Terms of Service