Meaning of this error

Hello All,

I am using this code -

<?php
$sql = "SELECT COUNT(id) FROM blog WHERE username='$u' and deleted='0' ";
$query=mysqli_query($db_conx,$sql);
$query_count2 = mysqli_fetch_row($query);
$votes = $query_count2[0];
$sql = "SELECT COUNT(view_count) FROM blog WHERE username='$u' and deleted='0' ";
$query_count2 = mysqli_fetch_row($query);
$votes = $query_count2[0];
$vcount=mysqli_query($db_conx,$sql);
$popularity=($votes/$vcount)*100;
if($popularity<0)
{$popularity="0";}
?> 

and getting a error:
Notice: Object of class mysqli_result could not be converted to int in C:\wamp\www\www.buddiescolumn.com\myblog.php on line 111

what does this mean

Personally I would start from scratch and get each part working at a time. For a start you have $query_count2 and $votes twice and the code seems all jumbled up.

I guess you are trying to convert some code into an integer
This is probably your problem $vcount=mysqli_query($db_conx,$sql); as this is code and not a result

1 Like

You are right:

$vcount=mysqli_query($db_conx,$sql); this was my problem. thanks

Isn’t his problem actually with the line of code below that?

$popularity=($votes/$vcount)*100;

He is using the $vcount variable (which will either contain FALSE or a mysqli_result object if the query was successful) in an integer context (as the divisor).

@shail_arya, your two queries could be amalgamated and your code could be simplified in general:

$sql = "SELECT COUNT(id), COUNT(view_count) FROM blog WHERE username = '{$u}' AND deleted = 0";
$query = mysqli_query($db_conx, $sql);

list($votes, $vcount) = mysqli_fetch_row($query);

if(($popularity = ($votes / $vcount) * 100) < 0)
  $popularity = 0;
2 Likes

This code seems to be perfect… But when i realizes now that i dont need the count of column. I need the individual sum of these columns. I googled and found that can be done using SUM but how to use to display is not clear… Can you help me on that too.

One more thing above you have used’ {$u '}, is it for some purpose or i can use normal '$u '.

Can you explain to me a little more about what you’re trying to achieve? Seeing your database structure and what data you’re trying to return from it would be a great help.

As for using the braces around the variable, it’s just my preferred method of interpolation (where variables are included into a string to be parsed). I find it a clearer syntax because the braces clearly delineate the variable from the surrounding string.

My database is like…

Id Username topic Likes View

Now a user can have more than one topics.
So i want to SUM all the likes of a user of his all topics to a variable X and SUM of all the view to variable Z

I believe if you replace the $q variable in my previous post with the following (using the column names as listed in your above post:

$q = "SELECT SUM(likes), SUM(View) FROM blog WHERE username = '{$u}' AND deleted = 0";

then that should work.

i used this

<?php
$sql = "SELECT SUM(likes), SUM(views) FROM blog WHERE username = '{$u}' AND deleted = 0";
$query = mysqli_query($db_conx, $sql);
list($likess, $viewss) = mysqli_fetch_row($query);
?>

and get this echoed as:

<?php echo $likess;?> <?php echo $viewss;?>

but nothing is visible.

Let’s try to debug your script. Can your paste the following line into your code to output the mysqli error (if there was one):

if(!$query)
  echo mysqli_error($db_conx);

Once you’re able to make it work, you might want to use prepared statements to prevent SQL injections. Here’s the official doc: http://php.net/manual/en/mysqli-stmt.bind-param.php