Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQLite count rows in table

Status
Not open for further replies.

gandalf458

IS-IT--Management
Jul 23, 2015
24
IT
I simply want to count the number of rows in a table. I have:

$query = 'SELECT count(*) FROM talks;';

but then how do I get the result of the select? I'm using PDO.

Thanks
 
I've tried

$query = 'SELECT count(*) FROM talks;';
$result = $db->query($query);

which I assume is much the same thing? But then I need to get the value out of $result!


I'm not a number, I'm a free man
 
Hi

Ok. Then fetch the data from the statement object and use it :
PHP:
[teal]<?php[/teal]

[navy]$db[/navy] [teal]=[/teal] [b]new[/b] [COLOR=orange]PDO[/color][teal]([/teal][i][green]'sqlite:gandalf458.sqlite'[/green][/i][teal]);[/teal]

[navy]$query[/navy] [teal]=[/teal] [i][green]'SELECT count(*) FROM talks;'[/green][/i][teal];[/teal]

[navy]$result[/navy] [teal]=[/teal] [navy]$db[/navy][teal]->[/teal][COLOR=orange]query[/color][teal]([/teal][navy]$query[/navy][teal]);[/teal]

[navy]$data[/navy] [teal]=[/teal] [navy]$result[/navy][teal]->[/teal][COLOR=orange]fetch[/color][teal]([/teal]PDO[teal]::[/teal]FETCH_ASSOC[teal]);[/teal]

[b]echo[/b] [i][green]"There are {$data['count(*)']} rows in the table\n"[/green][/i][teal];[/teal]

( You may prefer to give a nicer alias to the count(*) column. )

Feherke.
feherke.ga
 
Great - many thanks.


I'm not a number, I'm a free man
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top