mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given

shruti A
shruti A used Ask the Experts™
on
<?php include 'dbConfig.php';
 
       
      //$res=mysqli_query($db,"Select f.fname,f.sname,f.mail,f.num,f.gen,s.tadd,s.padd,s.amail,s.anum,s.status from first f,second s ");
        $sql2 = "Select f.fname,f.sname,f.mail,f.num,f.gen,s.tadd,s.padd,s.amail,s.anum,s.status from first f,second s";
               

                  $result2 = mysqli_query($db, $sql2) or die("Error: ".mysqli_error($db));
                 
echo "<tr><td colspan='5'></td></tr>";
             while($row = mysqli_fetch_array($result2))  
                          {  
                               echo '  
                               <tr>  
                                    <td>'.$row["f.fname"].'</td>  
                                    <td>'.$row["f.sname"].'</td>  
                                    <td>'.$row["f.mail"].'</td>  
                                    <td>'.$row["f.num"].'</td>  
                                    <td>'.$row["f.gen"].'</td>  
                                    <td>'.$row["s.tadd"].'</td>  
                                    <td>'.$row["s.padd"].'</td>  
                                    <td>'.$row["s.amail"].'</td>  
                                    <td>'.$row["s.anum"].'</td>  
                                    <td>'.$row["s.status"].'</td>  
                                     
                                     
                               </tr>  
                               ';  
                          }  
 
  ?>
  </table>
</div>
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
If you echo $result2 you'll see it's false.

Don't use die on your scripts. Treat your errors. Either with a try/catch or with a simple
if ($result2 == FALSE) printf("Error: %s\n", mysqli_error($db));

HTH,
Dan
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
The most common cause for that error is that the connection to the database server failed.
shruti Aweb developer

Author

Commented:
Im not getting what to do
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Jim RiddlesPrepress/OMS Specialist

Commented:
Add the following line immediately after creating your $db object:
var_dump($db);

Open in new window

Does it display a valid mysqli database object?
Technology Scientist
Commented:
What is happening is that there is an error in the query (invalid tables or something is not right in the SQL statement); so $results2 becomes false(boolean) instead of a mysqli_result object; therefore, you need to catch that error/return type before running code that requires a certain data type such as mysqli_fetch_array(). To do this you need a CONTROL STRUCTURE such as an if/else statement block (see line 6 below)

<?php 
	include 'dbConfig.php';
	//$res=mysqli_query($db,"Select f.fname,f.sname,f.mail,f.num,f.gen,s.tadd,s.padd,s.amail,s.anum,s.status from first f,second s ");
	$sql2 = "Select f.fname,f.sname,f.mail,f.num,f.gen,s.tadd,s.padd,s.amail,s.anum,s.status from first f,second s";
	$result2 = mysqli_query($db, $sql2);
	if($result2){
		echo "<tr><td colspan='5'></td></tr>";
		while($row = mysqli_fetch_array($result2))  
		{  
			echo '  
				<tr>  
					<td>' . $row["f.fname"]		. '</td>  
					<td>' . $row["f.sname"]		. '</td>  
					<td>' . $row["f.mail"]		. '</td>  
					<td>' . $row["f.num"]		. '</td>  
					<td>' . $row["f.gen"]		. '</td>  
					<td>' . $row["s.tadd"]		. '</td>  
					<td>' . $row["s.padd"]		. '</td>  
					<td>' . $row["s.amail"]		. '</td>  
					<td>' . $row["s.anum"]		. '</td>  
					<td>' . $row["s.status"]	. '</td>  
				</tr>  
			';  
		} 
	}else{
		echo "Error: " . mysqli_error($db);
	}
?>
  </table>
</div>

Open in new window

Jim RiddlesPrepress/OMS Specialist

Commented:
With no further input from the author, the solutions answer the question.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial