Jump to content

Warning: mysql_fetch_assoc() expects parameter 1 to be resource


Recommended Posts

we did an exercise in IT class it was about how to inject php code in css code we took a template as an example to work on. i injected php code in the page "services.php"as instructed but it didn't work and i keep getting this error : 

 

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\Entreprise\Entreprise\employee.class.php on line 21

 

unfortunately, the professor didn't correct this example so i hope someone will help here.

 

these are the codes i'm working on:

 

employee.class.php file:

 



<?php
   class Employee {
         public $codemp;
         public $nomemp;
         public $datnais;
         public $codfct;


function getEmployees() {
    $sql = "SELECT codemp, nomemp, datnais, codfct ";
    $sql .= "FROM employee ";
    $req = mysql_query($sql);
    return $req;
 }


function showHtmlEmployees($resource){
    print "<table border='1' width='60%'>";
    print "<tr>";
         print "<td>Employee Name</td>";
         print "<td>Birthday Date</td>";
    print "</tr>";


while ($data= mysql_fetch_assoc($resource)) {
   print "<li>";
       print "<h2>".$data['nomemp']."</h2>";
       print "<p>".$data['datnais']."</p>";
  print "</li>";
}


print "</table>"; 
}


}


 

Connection file:

 



<?php
//parameters
$server = "localhost" ;
$user = "root";
$password= "" ;
$database= "entreprise" ;


//connection
$db= mysql_connect($server,$user,$password);
$result= mysql_select_db($database,$db);
?>


 

 

 

services.php file:

 



<!DOCTYPE html>
<!-- Website template by freewebsitetemplates.com -->
<?php
include "connection.php";
include "employee.class.php";
$employee= New employee ();


?>
<html>
   <head>
     <title>Services - Chronokeep Website Template</title>
         <meta charset="utf-8">
         <link href="css/style.css" rel="stylesheet" type="text/css">
   <!--[if IE 7]>
         <link href="css/ie7.css" rel="stylesheet" type="text/css">
   <![endif]-->
  </head>


<body>
   <div id="background">
<div id="page">
  <div id="header"> <a href="index.html" id="logo"><img src="images/logo.png" width="295" height="55" alt="Chronokeep Website Template"></a>
   <ul class="navigation">
     <li>
       <a href="index.html">Home</a>
    </li>
    <li>
       <a href="about.html">About</a>
    </li>
     <li>
     <a href="blog.html">Blog</a>
    </li>
    <li>
      <a class="active" href="services.html">Services</a>
   </li>
   </ul>
 </div>
<!-- start of body-->
<div id="body">
  <div id="content">
     <h2>Services</h2>
       <ul id="article">
 <?php
$result=$employee->getEmployees();
$employee->showHtmlEmployees($result);
?>
       </ul>
   </div>
 </div>
</body>
</html>


 

 

the template is not mine ! we downloaded it to just work on it in class to better understand how php code is injected in a css file. 

I hope someone can help me with this. 

Edited by Ch0cu3r
Added code tags

Usually this type of error means your mysql query failed due to an error. You can use mysql_error to find out why its failing. Change lines 44 and 45 in services.php to the following

$result=$employee->getEmployees();

// if the query executed
if($result)
{
    $employee->showHtmlEmployees($result);
}
else
{
   // lets find out why the query failed
   trigger_error('Cannot get Employees from database: ' . mysql_error());
}

Please note when posting code wrap it within


tags (or click the <> button in the editor)

Edited by Ch0cu3r
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.