Couldn't fetch mysqli_row

mysql

#1

I have code I have thoroughly looked over and sent through many PHP checkers, but I always get these errors for my code:

[16-Jun-2018 22:11:09 UTC] PHP Warning:  mysqli_fetch_row(): Couldn't fetch mysqli_result in /home/willhoffman/public_html/view_post.php on line 24
[16-Jun-2018 22:11:09 UTC] PHP Warning:  mysqli_free_result(): Couldn't fetch mysqli_result in /home/willhoffman/public_html/view_post.php on line 81
[16-Jun-2018 22:19:00 UTC] PHP Warning:  mysqli_fetch_row(): Couldn't fetch mysqli_result in /home/willhoffman/public_html/view_post.php on line 25
[16-Jun-2018 22:19:00 UTC] PHP Warning:  mysqli_free_result(): Couldn't fetch mysqli_result in /home/willhoffman/public_html/view_post.php on line 82

No matter what I try I always get this. Here's my code:

    <?php
session_start();
require_once('required/db.php');
require_once('required/Parsedown.php');

$parsedown = new Parsedown();


if ($_GET['post_id'] == '') {
  echo '<div class="wrapper"><div class="container"><h1>Post Not Found</h1></div></div>';
}else {
  $pid = $_GET['post_id'];
  $sql="UPDATE posts SET view_counter=view_counter + 1 WHERE id=$pid";
  mysqli_query($con,$sql);
  $sql="SELECT title,author,img_path,body,view_counter,tags,prev_text FROM posts WHERE id = $pid";
  $result = $con->query($sql);
  
  if ($result=mysqli_query($con,$sql))
{





while ($row=mysqli_fetch_row($result))
  {
    echo '
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width,initial-scale=1">
  
    <title>'.$row[0].'</title>
  <link rel="stylesheet" href="assets/css/style.css" type="text/css">
  <meta name="description" content="'.$row[6].'">
  <meta name="keywords" content="'.$row[5].'">
  <meta name="author" content="'.$row[1].'">

  </head>
  <body>
  '; 
  require('assets/nav.php');
  echo '
    <div class="flol"><h1>' . $row[0] . '</h1><br><h3>By: ' . $row[1] . ' with '. $row[4] .' views</h3><br><br><img style="width:auto; height:300px;" src="' . $row[2] . '"><br><br><br>';
    
    echo '<div class="addthis_inline_share_toolbox_judp"></div>';
    echo $parsedown->text($row[3]);
    echo '';
    echo '</div>';
    echo '<div class="flol top-margin bottom-margin"><h1>Other Posts</h1></div>';
    $sql="SELECT id,title,author,prev_text,img_path,tags,post_date FROM posts ORDER BY RAND() LIMIT 5";

      if ($result=mysqli_query($con,$sql))
    {
    // Fetch one and one row
    while ($row=mysqli_fetch_row($result))
      {
      	
	   printf('<div class="flol"><a href="view_post.php?post_id=%s"><div class="post_container">
	          <img src="%s" height="100" width="100" style="float:left;">
	          <div style="padding-left:200px;">
	          <h1>%s</h1>
	          <h3>Post #%s by %s</h3>
	          <br>
	          <p>%s</p>
	          <br>
	          <p><i class="fa fa-tag"></i> %s</p>
	          <h5>Posted on %s</h5>
	          </div>
	        </div></a></div>
	        ', $row[0], $row[4], $row[1], $row[0], $row[2], $row[3], $row[5], $row[6]);
	        //REWRITE when needed! ^
    }

    // Free result set
    mysqli_free_result($result);
    }
}

// Free result set
mysqli_free_result($result);
}

mysqli_close($con);

}






 ?>
<div id="awn-z2043303"></div>
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-5b19c3d1ad5926b6"></script>
  </body>
</html>

#2

I don't do mysqli myself, but these two lines

  $result = $con->query($sql);
  
  if ($result=mysqli_query($con,$sql))

seem to be the same - they both execute the query, don't they? Is there a reason you do that twice?

But even if that's not it, inside your first while() loop, you then execute a second query, but use the same variable name $result for the results object. That can't work, as it will overwrite the one that is on the outside of the loop. You also use the same $row array to retrieve each row, which might work but might be confusing.

If that's no help, please identify which are the lines in your errors - I can easily count to 24 but keep getting lost for the later one.


#3

The last mysqli_free_result($result); seems to be where the problem lies


#4

Yes, because you've already called the same function on that result object, inside the loop. You can't use the same name while you're still using the one outside the loop.


#5

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