PDA

View Full Version : Trying to Make a simple counter but its not working


Bry Man
08-20-2005, 11:29 PM
Hey,

I figured id try and make a simple little counter for my site using php/sql, but i cant seem to get it to work, its buggin the crap out of me, im fairly new to Sql and am just trying things out here and there. This is what I have for script so far.

<?php
$username="root";
$password="";
$database="SiteDatabase";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM counter WHERE id=1";
$count=mysql_query($query);
print("$count");
$count++;
$query = "UPDATE counter SET count=$count WHERE id=1";
mysql_query($query);
mysql_close();
print("$count");
?>

And the database table for that

$query="CREATE TABLE counter (
id int(1) NOT NULL,
count int(10) NOT NULL,
PRIMARY KEY (id),
KEY count (count)
)";


and it will print 'Resource id #2' to the browser, but nothing else and the table remains unchanged which makes me think that im screwing up on the updating part =S, probably on the retriveing part as well...hell the whole thing may be wrong for all i know

And to clear something up, I changed the value of the 'id' to 1 inside phpmyadmin, Im not just assuming its 1 because of this "id int(1) NOT NULL,"....im not that new...but perhaps its something simple like that. Any help is appreciated

jaap
08-21-2005, 02:55 PM
Hi Bry Man,

first of all: remove the @ in line 7. The @ suppresses errors and when you're developing a site, you don't want that.

Second: you are using the variable $count on multiple ways. Try to make a difference between your variables.

Third: when using queries, you want to 'fetch' the results. All you get back is a result number. Try to read something about this in the php manual pages, because these basics are pretty essential.


Now, for a script that will work:
<?php
$username="root";
$password="";
$database="SiteDatabase";

$link = mysql_connect(localhost, $username, $password) or die("Couldn't connect");
mysql_select_db($database)or die("Couldn't select Database");
$sql = "SELECT * FROM `counter` WHERE `id`='1' LIMIT 1;";
$result = mysql_query($sql);
while($row = mysql_fetch_object($result)){
$intCountValue = $row->count;
}
print($intCountValue . "<br />");

$intCountValue++;
$updatesql = "UPDATE `counter` SET `count`='".$intCountValue."' WHERE `id`='1' LIMIT 1;";
$query="SELECT * FROM counter WHERE id=1";
if(!mysql_query($updatesql)){
print("Query error: ".mysql_error()."<br />");
} else {
print("query worked out ok. <br />");
print($intCountValue);
}
?>


I am not saying this is a good script. But this way, it's more like your script.

Oh, and by the way: configure mysql to use with a separate username WITH password. It might not be important in this stage, but you'll see the reasons later, as you develop more pages.

Good Luck,

Jaap

Bry Man
08-21-2005, 08:08 PM
Hey

I redid the script with the new stuff you showed me and it now works. I think my main problem was that being the tard I am...I had forgotten to create the row with the id cell=1...I had it before but I droped the table and recreated it for some reason :rolleyes: so, there was nowhere for the count value to be stored...now that thats all fixed and the script is modified it works like a charm, thanks for the help. Here is the script that I came up with

<?php
$username="root";
$password="";
$database="SiteDatabase";

mysql_connect(localhost,$username,$password);
mysql_select_db($database) or die( "Unable to select database");

$query="SELECT * FROM counter";
$result=mysql_query($query);
while ($row = mysql_fetch_object($result)) {
$count=$row->count;
}

$count++;
$query = "UPDATE counter SET count='".$count."' WHERE id='1'";

mysql_query($query);
mysql_close();
print("$count");
?>

Ya, the username and password thing will be changed, this is just on localhost right now so, like you said, at this stage that doesnt really matter, but later on it will be changed.

Again, thanks for the help :thumbsup: