Click here to Skip to main content
15,889,877 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have an errors it says
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given


What I have tried:

home.php

 <?php 
include("auth.php");

 ?>



<!DOCTYPE html>
<head>
     
        <title>Tamarga | Forum</title>
  
        <link rel="shortcut icon" href="../favicon.ico"> 
       <link rel="stylesheet" type="text/css" href="css/style.css">
       <style>
			body
				{
					background-image: url('img/indexback.jpg');
					background-attachment: fixed;
					background-size: 100% 100%;
				}
		</style>
    </head>
    <body>



	
<ul class="topnav">
 <li style="float:right"><a href="logout.php" float="right">Logout</a></li>
    <li style="float:right"><a>Hello <?php echo $_SESSION['Username']; ?>!</a></li>
    <li><a class="active" href="#home">Tamarga Forum</a></li>
    <li><a href="#home">Home</a></li>



</ul>

  <br> <br>
<center>
<div class="btn-group">
  <button class="button">Post Your Comments</button>
  </div>

</center>




<center>

<br>
<table>
<tr>
    <th>Category</th>
    <th>Description</th>
    </tr>


<?php

include 'dbconnect.php';
$sql="SELECT cat_name,cat_desc FROM tblcategories";

$query=mysql_query($sql);
while ($users=mysql_fetch_assoc($query)){
    echo "<tr>";
    echo "<td>".$users['cat_name']."</td>";
    echo "<td>".$users['cat_desc']."</td>";
    echo "</tr>";

}
?>
</table>
   </center> 
     </div>
    </body>
</html>
Posted
Updated 3-Jul-20 10:45am
Comments
[no name] 6-Mar-17 8:41am    
Looks to me that your query result is not a boolean value that you while expects to have.

Some error must have occurred whele querying the database, you can get it to tell by adding a error handling like this:
$query=mysql_query($sql);

if (!$query){
   die('Invalid query: '.mysql_error());
}

while ($users=mysql_fetch_assoc($query)){
  //omitted
}
However, take note[^] that:
Quote:
This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.
 
Share this answer
 
v2
The return value of mysql_query is a mixed type. It is a boolen value or a resource upon success for specific query types.

When an error occured, the return value is boolean false which probably happened here.

Solution:
Check the return value before fetching results:
PHP
if ($query = mysql_query($sql)) {
    while ($users=mysql_fetch_assoc($query)){
        // ...
    }
} else {
    // Report error
}
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900