1

I'm having some issues with my SQL Query. I am trying to run an sql query that saves the output into a variable and then print the variable in a different section of code:

Query:

$sql = "select Count(distinct `Customer Name`) as columnNameCount from allservers";
$result = mysqli_query($DBcon, $sql);

Display variable:

<h3 align="center"><?php echo $resultarr;?></h3>

Error message:

Catchable fatal error: Object of class mysqli_result could not be converted to string

Dharman
  • 30,962
  • 25
  • 85
  • 135

2 Answers2

0
<?php
$sql = "select Count(distinct `Customer Name`) as columnNameCount from allservers";
$result = mysqli_query($DBcon, $sql);
$row = mysqli_fetch_row($result);
?>
<h3 align="center"><?php echo $row[0];?></h3>
0

Remember: Mysqli_query() return an Object Resourse to your variable $result! Not a String!

You can't directly use it as $result variable! If you have multiple results, you can loop:

while ($row = $result->fetch_assoc())
    echo $row['some_row'];

else you have to fetch as row and display according to index:

$row = $result->fetch_row(); 
echo $row[0]; // $row[index]

Read The Documentaion

Harish ST
  • 1,475
  • 9
  • 23