0

if I use the following code everything works:

function likecount($id) {
$link = mysqli_connect('localhost', 'root', '', 'printstagram');
    $query = $link->query("SELECT likes FROM photos WHERE id='$id'");
    $a = mysqli_fetch_row($query);
    $b=$a[0];
    echo $b;
}

if I use a file called dbconfig.php which has the following code:

$link = mysqli_connect('localhost', 'root', '', 'printstagram');

and then I include dbconfig.php on the php page where I use the likecount function, then uses the global keyword:

function likecount($id) {
    global $link;
    $query = $link->query("SELECT likes FROM photos WHERE pid='$id'");
    $a = mysqli_fetch_row($query);
    $b=$a[0];
    echo $b;
}

I got an error:mysqli_fetch_row() expects parameter 1 to be mysqli_result, boolean given.

Can someone tell me where could the error come from? The function is responsible for counting and updating the number of likes. If I use mysqli_connect every time the function is called, I will use mysqli_connect many times in a page, is that an issue? Comparing with global $link, i believe establishing a connection every time the function is called will add more pressure to the server.

I conducted some further testing, for the following code:

<?php
include('dbconfig.php');
global $link;
echo $link;
?>

I got:Catchable fatal error: Object of class mysqli could not be converted to string. Is this considered an ordinary behavior?

I conducted another testing, the following code will work smoothly(but it will establish a connection every time the function is called):

<?php

function likes2($id) {
  $link = mysqli_connect('localhost', 'root', '', 'printstagram');

    $query = $link->query("SELECT likes FROM photos WHERE pid='$id'");

    $a = mysqli_fetch_row($query);
    $b=$a[0];
    echo $b;

}
?>

but the following doesn't:

<?php
$link = mysqli_connect('localhost', 'root', '', 'printstagram');
function likes2($id) {

    global $link;
    $query = $link->query("SELECT likes FROM photos WHERE pid='$id'");
    $a = mysqli_fetch_row($query);
    $b=$a[0];
    echo $b;

}
?>

it gives the error: mysqli_fetch_row() expects parameter 1 to be mysqli_result, boolean given

gravition
  • 113
  • 1
  • 3
  • 13
  • error saying your query has failed to execute – Arun Kumaresh Jul 20 '17 at 05:04
  • I know that but I want to know why it failed. I looked at this answer: https://stackoverflow.com/questions/10836078/mysqli-mysql-query-inside-function-not-working and I don't know why their codes works but mine doesn't – gravition Jul 20 '17 at 05:07
  • There is nothing wrong with your code as far as I can tell, it likely due to your database, show your `photo` database schema or database content. The error is suggesting that your `mysql_fetch_row($qeury)` didn't yield any result. – hcheung Jul 20 '17 at 05:33
  • Hi hchueng, if there is something wrong with my database, it shouldn't work "optionally". the problem is as long as I establish a new mysqli connection every time the function is called, everything works smoothly, but if I use global $link it doesn't – gravition Jul 20 '17 at 05:42
  • What does `echo $link->error();` print out when you put it inside your `likecount` function just after the link with `$link->query()`? – Jirka Hrazdil Jul 20 '17 at 05:52
  • call to undefined method mysqli:error() – gravition Jul 20 '17 at 05:58
  • @gravition: sorry for that, it should be just `echo $link->error;` – Jirka Hrazdil Jul 20 '17 at 06:01
  • @JiriHrazdil I got : Commands out of sync, you can't run this command right now – gravition Jul 20 '17 at 06:06
  • Great, do a Stack Overflow search with this error message and find yourself a solution (as it is hidden in the code you have not shown us, we cannot be of any more help here). – Jirka Hrazdil Jul 20 '17 at 06:08

2 Answers2

0

this is index.php like that

require "dbconnect.php";
function likecount($item_id) {
    global $link;
    $query = $link->query("SELECT likes FROM photos WHERE pid='$item_id'");
    $a = mysqli_fetch_row($query);
    $b=$a[0];
    echo $b;
}
likecount($item_id);

this is dbconnect.php

$link = mysqli_connect('localhost', 'root', '', 'printstagram')
0

I have checked your both code are working fine!. The problem may with your database name, you using two different database "test" and "printstagram" may be "printstagram" photos table of database printstagram dont have records for which id you are searching.

You can check if there is record like this.

if($a = mysqli_fetch_row($query)){
    $b=$a[0];
    echo $b;
}