Uncaught error call to undefined method mysqli_stmt rowcount

I have a problem switching from MYSQL to MYSQLi. The codes work fine with MYSQL but when i change the connection to MYSQLi, I received the error as stated above when I'm fetching my query. How can i fetch my queries using mysqli functions?

Code:

        function __construct(){
    $this->link = mysqli_connect('localhost', 'root', '', 'ajax_rating');

    if (!$this->link) {
     die('Connect Error (' . mysqli_connect_errno() . ') '
        . mysqli_connect_error());
    }

    echo 'Success... ' . mysqli_get_host_info($this->link) . "\n";

                }

    function getItems($id = null){
        if(isset($_GET['id']))
        {
            $query = $this->link->query("SELECT * FROM items WHERE id = '$id'");
        }
        else
        {
            $query = $this->link->query("SELECT * FROM items");
        }
        $rowCount = $query->rowCount();
        if($rowCount >= 1)
        {
            $result = $query->fetchAll();
        }
        else
        {
            $result = 0;
        }
        return $result;

Uncaught error call to undefined method mysqli_stmt rowcount

asked Jul 20, 2015 at 3:48

0

Use num_rows

Read MySQLi row_count

So final answer would be

function getItems($id = null)
{
    if(isset($_GET['id']))
    {
        $query = $this->link->query("SELECT * FROM items WHERE id = '$id'");
    }
    else
    {
        $query = $this->link->query("SELECT * FROM items");
    }
    $rowCount = $query->num_rows;//change here
    if($rowCount >= 1)
    {
        $result = $query->fetchAll();
    }
    else
    {
        $result = 0;
    }
    return $result;
}

EDIT 01

use mysqli_fetch_all instead of fetchAll()

mysqli_fetch_all($query,MYSQLI_ASSOC);

so answer world be

    if($rowCount >= 1)
    {
        $result = mysqli_fetch_all($query,MYSQLI_ASSOC);
    }

answered Jul 20, 2015 at 3:55

Uncaught error call to undefined method mysqli_stmt rowcount

Abdulla NilamAbdulla Nilam

32.3k16 gold badges60 silver badges80 bronze badges

2

you can use num_rows for counting rows in DB and you can direct call connection like this :-

//for connection

$con = mysqli_connect("localhost", "root", "", "ajax_rating");
if(!$con)
{
echo "connection error";
}


//query
function getItems($id = null)
 {
if(isset($_GET['id']))
{
    $query = mysqli_query($con,"SELECT * FROM items WHERE id = '$id'");
}
else
{
    $query = mysqli_query($con,"SELECT * FROM items");
}
if (mysqli_num_rows($query) > 0)//change here
    {
    while($row = mysqli_fetch_assoc($query))
     {
       $result=$row;
     }

}
else
{
    $result = 0;
}
return $result;
}

answered Jul 20, 2015 at 5:19

SakuraSakura

931 gold badge1 silver badge10 bronze badges

Not the answer you're looking for? Browse other questions tagged php mysqli count fetch or ask your own question.

973 votes

2 answers

Uncaught error call to undefined method mysqli_stmt rowcount

Uncaught error call to undefined method mysqli_stmt rowcount

Uncaught error call to undefined method mysqli_stmt rowcount

Get the solution ↓↓↓

I have a problem switching from MYSQL to MYSQLi. The codes work fine with MYSQL but when i change the connection to MYSQLi, I received the error as stated above when I'm fetching my query. How can i fetch my queries using mysqli functions?

Code:

        function __construct(){
    $this->link = mysqli_connect('localhost', 'root', '', 'ajax_rating');

    if (!$this->link) {
     die('Connect Error (' . mysqli_connect_errno() . ') '
        . mysqli_connect_error());
    }

    echo 'Success... ' . mysqli_get_host_info($this->link) . "\n";

                }

    function getItems($id = null){
        if(isset($_GET['id']))
        {
            $query = $this->link->query("SELECT * FROM items WHERE id = '$id'");
        }
        else
        {
            $query = $this->link->query("SELECT * FROM items");
        }
        $rowCount = $query->rowCount();
        if($rowCount >= 1)
        {
            $result = $query->fetchAll();
        }
        else
        {
            $result = 0;
        }
        return $result;

2022-05-13




259

votes

Uncaught error call to undefined method mysqli_stmt rowcount

Uncaught error call to undefined method mysqli_stmt rowcount

Answer

Solution:

Usenum_rows

Read MySQLi row_count

So final answer would be

function getItems($id = null)
{
    if(isset($_GET['id']))
    {
        $query = $this->link->query("SELECT * FROM items WHERE id = '$id'");
    }
    else
    {
        $query = $this->link->query("SELECT * FROM items");
    }
    $rowCount = $query->num_rows;//change here
    if($rowCount >= 1)
    {
        $result = $query->fetchAll();
    }
    else
    {
        $result = 0;
    }
    return $result;
}

EDIT 01

use instead offetchAll()

mysqli_fetch_all($query,MYSQLI_ASSOC);

so answer world be

    if($rowCount >= 1)
    {
        $result = mysqli_fetch_all($query,MYSQLI_ASSOC);
    }

Undefined answered

2022-05-13

Link to answer




732

votes

Uncaught error call to undefined method mysqli_stmt rowcount

Uncaught error call to undefined method mysqli_stmt rowcount

Answer

Solution:

you can use num_rows for counting rows in DB and you can direct call connection like this :-

//for connection

$con = mysqli_connect("localhost", "root", "", "ajax_rating");
if(!$con)
{
echo "connection error";
}


//query
function getItems($id = null)
 {
if(isset($_GET['id']))
{
    $query = mysqli_query($con,"SELECT * FROM items WHERE id = '$id'");
}
else
{
    $query = mysqli_query($con,"SELECT * FROM items");
}
if (mysqli_num_rows($query) > 0)//change here
    {
    while($row = mysqli_fetch_assoc($query))
     {
       $result=$row;
     }

}
else
{
    $result = 0;
}
return $result;
}

Undefined answered

2022-05-13

Link to answer




People are also looking for solutions of the problem: undefined array key
Source

Share


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.


Similar questions

Find the answer in similar questions on our website.