0

I can't retrieve values from SQL with the use of PHP in cloud9(C9). I am getting the following error (warning).

mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in /home/ubuntu/workspace/index.php

<?php 
session_start();
$conn = mysqli_connect("127.0.0.1", "mehul87", '', "omnicat");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
$sql= "select * from iron_study where Element='Serum_iron'";
$array=mysqli_query($conn,$sql) ;
$data=mysqli_fetch_assoc($array);
$optimal=$data['Optimal'];
$Min=$data['Min'];
$Max=$data['Max'];
echo $Min;
echo $Max;
$sql1="select * from iron_study where Element='Transferrin_IBC'";
$array1=mysqli_query($conn,$sql1);
$data1=mysqli_fetch_assoc($array1);
$optimal1=$data1['Optimal'];
$Min1=$data1['Min'];
$Max1=$data1['Max'];
echo $Min1;
echo $Max1;
?>
Just a student
  • 10,560
  • 2
  • 41
  • 69

1 Answers1

0

There might be something incorrect with your query statement? I don't mean the syntax, that's fine, but maybe something you are searching for isn't spelled right?

mysqli_fetch_assoc ( mysqli_result $result ) Returns an associative array that corresponds to the fetched row or NULL if there are no more rows.

If the mysqli_result of your search is empty, I believe it automatically sets the 1st parameter (which is the search result variable $array1 in your case) to null.

If I were you, I would change:

$array1=mysqli_query($conn,$sql1);

To this:

$array1=mysqli_query($conn,$sql1) or die($conn->error);

This will echo the MySQL response error if there is one. Additionally, I would add:

if(mysqli_num_rows($array1) == 0){
echo 'There are no results found';
}

This check if you actually have search results from your query.