# PHP Mysql counter

**URL:** <https://forum.kirupa.com/t/php-mysql-counter/214959>\
**Category:** programming\
**Created:** [February 2, 2007, 12:43pm UTC](https://forum.kirupa.com/t/php-mysql-counter/214959 "2007-02-02T12:43:07Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![pucca](https://avatars.discourse-cdn.com/v4/letter/p/5f8ce5/32.png) [@pucca](https://forum.kirupa.com/u/pucca)\
**Post date:** [February 2, 2007, 12:43pm UTC](https://forum.kirupa.com/t/php-mysql-counter/214959/1 "2007-02-02T12:43:07Z")

</div>

Hi, I’ve got a counter that connects to a database… But it’s not working… Can somebody please tell me why, or maybe you have another script for it :

```php
//part1
$page=$_SERVER['PHP_SELF'];

//part2

include('db_connect.php');

//part4
$result = mysql_query("SELECT * FROM counter WHERE page='$page'",$connnews);

$sql="";
$count=1;

if(mysql_num_rows($result)==0)//we don't have a record for it
{

//part5
$sql=<<<SQL
INSERT INTO counter VALUES(
'$page',
1
)
SQL;

}
else if(mysql_num_rows($result)>0)
{

//part6
$sql=<<<SQL
UPDATE counter SET count=count+1 WHERE page='$page'
SQL;
$array=mysql_fetch_assoc($result);

$count=$array['count'];

}

//part7
$newResult = mysql_query($sql,$connnews);

//part8
print $count+1;

mysql_close($connnews);

```

thanks
