0

I'm trying to make a script in PHP (using mariadb) that displays the amount of rows in a table and I'm trying to use the following script:

echo $conn->query('SELECT COUNT(*) FROM testtable')->fetchObject()->count(*);

But when I execute it, I get the following error:

Parse error: syntax error, unexpected '*'. How can I get around this? How can I resolve it

Mittal Patel
  • 2,659
  • 12
  • 23
coolcuber
  • 21
  • 3

4 Answers4

2

The code

echo $conn->query('SELECT COUNT(*) AS rownum FROM testtable')->fetchObject()->rownum;

Worked for me

coolcuber
  • 21
  • 3
  • That's true, using an alias will work, but since you're just getting a single value still, fetchColumn still makes more sense than using fetchObject. – Devon Jul 06 '18 at 17:40
1

Use fetchColumn when you only need to get one value.

echo $conn->query('SELECT COUNT(*) FROM testtable')->fetchColumn();

Your error is because count(*) would calling a method named count on the object with a first argument of *. Just an asterisk without quotes will result in a syntax error in PHP. You instead would need to use $obj->{'COUNT(*)'} to access a property with the name of COUNT(*), but it's much simpler to use fetchColumn.

Devon
  • 32,773
  • 9
  • 61
  • 91
0

Have you tried like this? with fetchColumn()

$result = $conn->query("SELECT COUNT(*) FROM testtable");
echo $result->fetchColumn();

See example: http://php.net/manual/en/pdostatement.fetchcolumn.php#72609

Always Sunny
  • 32,751
  • 7
  • 52
  • 86
  • When I did, I got 'Object of class stdClass could not be converted to string' – coolcuber Jul 06 '18 at 17:17
  • @coolcuber I made a mistake you need fetchColumn here. see my edit plz – Always Sunny Jul 06 '18 at 17:20
  • @Devon sorry to say, I didn't copy your answer. I just forgot the reference and method name. See I've added the reference of php.net – Always Sunny Jul 06 '18 at 17:23
  • OK. It just looks odd that you had a completely different answer when you first answered the problem but put the same method in 3 minutes after I added my answer. – Devon Jul 06 '18 at 17:26
  • @Devon I agreed and I really apologize for that "I just forgot fetch__() method name here" but when I see the php.net ref. I just edit it back. Sorry for the confusion :) Hope you understand – Always Sunny Jul 06 '18 at 17:29
  • @Devon I started to edit my answer when coolcuber said that "When I did, I got 'Object of class stdClass could not be converted to string' ", then I realize I've used wrong method here :( – Always Sunny Jul 06 '18 at 17:32
0

The error is due to the last asterisk in the ->count(*) but even if this were fixed as Devon pointed out, it wouldn't work anyway since there is no count() method on what is returned by fetchObject().

Refer to the documentation for fetchObject here and fetchColumn here.

You could try

echo $conn->query('SELECT COUNT(*) FROM testtable')->fetchObject();

To get the whole row, but since the row has just one column, ->fetchColumn(0) is all you need.

geco17
  • 4,824
  • 1
  • 16
  • 32