PHP/sqlite3 rowcount question
#1

Hello there,
I know this is not really pawn. But it is pawn related.
I am creating a user control panel and i want to list reports on the website for admins.
This code works perfect but i have trouble with page navigation.
If i use the same way as in samp like count++ it always returns 1.

How do i succesfully count rows? because numrows and sqlite_num_rows dont work.

PHP код:
$record_index= ($page-1) * $limit;  
 
$sql "SELECT * FROM REPORTS LIMIT $record_index$limit"
 
 
$stmt $db->query($sql);
 while(
$row $stmt->fetchArray(SQLITE3_ASSOC) )
 {
        
$countrows $stmt->numRows(); // error 
I did try to follow these :
http://php.net/manual/en/function.sqlite-num-rows.php

But this always result into a fatal error
Код:
Fatal error: Call to undefined method SQLite3Result::numRows() in D:\AppServ\www\ucp\reports.php on
Any help would be appreciated
Reply
#2

I have fixed it fortunately.
If you may run into this problem. This is a fix:

PHP код:
<?php
    
function SqliteNumRows($query){
        
$numRows 0;
        while(
$rows $query->fetchArray()){
            ++
$numRows;
        }
        return 
$numRows;
    }
?>
$countrows = SqliteNumRows($stmt);
//edit: Otherwise it shows 4 instead of 5 when having 5 rows.
$countrows += 1;
Reply
#3

Quote:
Originally Posted by jasperschellekens
Посмотреть сообщение
I have fixed it fortunately.
If you may run into this problem. This is a fix:

PHP код:
<?php
    
function SqliteNumRows($query){
        
$numRows 0;
        while(
$rows $query->fetchArray()){
            ++
$numRows;
        }
        return 
$numRows;
    }
?>
$countrows = SqliteNumRows($stmt);
did u try this?

PHP код:
while($row $stmt ->fetch_object()) 
{
        
$countrows $row->num_rows
I know u fixed it, but i wanna know if this works/fixes it also.
Reply
#4

Quote:
Originally Posted by kingmk
Посмотреть сообщение
did u try this?

PHP код:
while($row $stmt ->fetch_object()) 
{
        
$countrows $row->num_rows
I know u fixed it, but i wanna know if this works/fixes it also.
I don't think so but im not sure. i use $row = $stmt->fetchArray(SQLITE3_ASSOC); to fetch in this case.

Also my way didnt exactly fix it.
When you have 5 rows it shows 4.
So to actually fix it now it must be:
PHP код:
$countrows SqliteNumRows($stmt); 
  
$countrows += 1
Reply
#5

Quote:
Originally Posted by ******
Посмотреть сообщение
PHP код:
SELECT COUNT(*) FROM `reports`; 
will also return 1 in sqlite/php.
Reply


Forum Jump:


Users browsing this thread: 5 Guest(s)