Author Topic: database query help  (Read 884 times)

0 Members and 1 Guest are viewing this topic.

Offline blainec

  • Senior Member
  • **
  • Posts: 70
    • Weather YYC
database query help
« on: April 16, 2022, 08:21:33 PM »
I'm new to sql and PHP but I'm trying ;)

I use mariadb. I am  trying to query information_schema.tables [tablename] TABLE_ROWS. to get the number of rows.
I have tried using COUNT function,  but that doesn't seem to work for me, s o I thought this would give me the info I need.

I hope there is a  SQL wizard out there that can help me...

Thanks
Blaine

Offline davidefa

  • Forecaster
  • *****
  • Posts: 436
Re: database query help
« Reply #1 on: April 17, 2022, 05:10:51 AM »
I think you can use something like this ( insert appropriate values for host, username, password, db_name and table_name ):

Code: [Select]
$link = mysqli_connect("host", "username", "password","db_name");
$result = mysqli_query($link, "SELECT count(*) FROM table_name");
$num_rows = mysqli_fetch_row($result)[0];
echo "table_name rows: $num_rows\n";

Offline blainec

  • Senior Member
  • **
  • Posts: 70
    • Weather YYC
Re: database query help
« Reply #2 on: April 17, 2022, 09:39:34 AM »
not returning anything

Offline bchwdlks

  • Senior Contributor
  • ****
  • Posts: 196
Re: database query help
« Reply #3 on: April 17, 2022, 01:36:28 PM »
I use a mariadb database on an RPi. It is feeding a Meteotemplate web page. My work career involved many years of Unix databases so I almost always use command line utilities. This is simple record count from a single table


pi@mcgee: ~
$  mysql -u USERNAME -D davis -A --password=PASSWORD -e "select count(*)  from alldata where DateTime > '2020-01-01 00:00:01';"
+----------+
| count(*) |
+----------+
|   235661 |
+----------+

Offline mcrossley

  • Forecaster
  • *****
  • Posts: 1140
    • Wilmslow Astro
Re: database query help
« Reply #4 on: April 18, 2022, 03:50:06 AM »
not returning anything

Have you checked your mysql log files for any errors?
Mark

Offline davidefa

  • Forecaster
  • *****
  • Posts: 436
Re: database query help
« Reply #5 on: April 18, 2022, 04:37:28 PM »
a) you can execute the 'select' ( SELECT count(*) FROM table_name ) in phpmyadmin, it should return 1 value
b) you can execute the following code ( only added a few debug prints )

Code: [Select]
<?php
$link 
mysqli_connect("host""username""password","db_name");
echo 
"connect error: ".mysqli_error($link)."<br>\n";
$result mysqli_query($link"SELECT count(*) FROM table_name");
echo 
"query error: ".mysqli_error($link)."<br>\n";
$num_rows mysqli_fetch_row($result)[0];
echo 
"fetch_row error: ".mysqli_error($link)."<br>\n";
echo 
"table_name rows: $num_rows<br>\n";
?>

P.S.
insert appropriate values for host, username, password, db_name and table_name