 |

05-13-2004, 02:36 PM
|
 |
WebProWorld Pro
|
|
Join Date: Nov 2003
Location: -_-
Posts: 187
|
|
Counter driving me nuts
Ok, heres my script (PHP) im trying to use to make a counter (gets count from database, adds 1, puts that back in database, and shows the number) but, i have an error somewhere (I get a blank page) but i cant find it, any help?
<?php
$user = "user";
$host = "host";
$password = "password";
$database = "database";
$connection = mysql_connect($host,$user,$password) or die ("Couldn't connect to server");
$db = mysql_select_db($database,$connection)
or die ("Couldn't select database");
$query = "SELECT Counter FROM Counted";
$result = mysql_query($query,$connection)
or die("Couldn't execute query.");
$Counter = +1;
echo $Counter;
$query = "SELECT Counter FROM Counted";
$query = "UPDATE Counted WHERE Counter = '$Counter'";
or die ("Couldn't execute other query.");
?>
__________________
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
|- >(^_^)> | (t^^t) | <(^_^)< -|
- - - - - - - - - - - - - - - -
|

05-13-2004, 06:16 PM
|
 |
WebProWorld Pro
|
|
Join Date: Jul 2003
Location: UK
Posts: 261
|
|
Quote:
$query = "UPDATE Counted WHERE Counter = '$Counter'";
or die ("Couldn't execute other query.");
|
You put a ; after '$Counter'"
That shouldn't be there. I hate it when that happens :P I always forget to put ; after I've echo'd something.
echo "Damn! I missed it out"
Grrrrrrrrrrrr.
|

05-14-2004, 02:24 PM
|
 |
WebProWorld Veteran
|
|
Join Date: Feb 2004
Location: Queen Charlotte B. C. Canada
Posts: 351
|
|
I see many things that confuse me in your script the main error I see is that $counter is never innialized.
With out knowing the struture of you table it is difficut to write a script for it but try the following script.
<?php
$user = "user";
$host = "host";
$password = "password";
$database = "database";
$connection = mysql_pconnect($host,$user,$password) or die ("Couldn't connect to server");
mysql_select_db($database) or die ("Couldn't select database");
$query = "SELECT Counter FROM Counted";
$result = mysql_query($query)
or die("Couldn't execute query.");
while ($row = mysql_fetch_array ($result))
{$counter=$row[0];}
$Counter++;
echo $Counter;
$mysql_query("UPDATE Counted SET Counter = '$Counter'")
or die ("Couldn't execute other query.");
?>
__________________
Irony: That for most people the most "trusted" web site on the planet is for a company the has been convicted of criminal activity.
Both Security and SuSe start with "S". www.oldslides.com
|

05-14-2004, 02:32 PM
|
 |
WebProWorld Veteran
|
|
Join Date: Feb 2004
Location: Queen Charlotte B. C. Canada
Posts: 351
|
|
Sorry minnor typo.
{$counter=$row[0];}
should be
{$Counter=$row[0];}
__________________
Irony: That for most people the most "trusted" web site on the planet is for a company the has been convicted of criminal activity.
Both Security and SuSe start with "S". www.oldslides.com
|

05-14-2004, 02:41 PM
|
 |
WebProWorld Pro
|
|
Join Date: Aug 2003
Location: France
Posts: 196
|
|
From my point of vue, after you have sent your SQL request you don't read the returned values. So your $Counter will always be undefined.
You should add this to your code (in red) :
----
$result = mysql_query($query,$connection)
or die("Couldn't execute query.");
$line=mysql_fetch_array($result);
$Counter=$line['Counter'];
$Counter = +1;
...
----
(assuming that your SQL request returned only 1 line of datas from your table, and that your field with the counter is called "Counter" within that same table)
Jean-Pierre
|

05-14-2004, 05:57 PM
|
|
WebProWorld Pro
|
|
Join Date: Feb 2004
Location: Colorado
Posts: 159
|
|
Running off of what you posted, there are a couple of things that don't quite work...
(my comments // correction are in bold)
<?php
$user = "user";
$host = "host";
$password = "password";
$database = "database";
$connection = mysql_connect($host,$user,$password) or die ("Couldn't connect to server");
$db = mysql_select_db($database,$connection) or die ("Couldn't select database");
$query = "SELECT Counter FROM Counted";
$result = mysql_query($query,$connection) or die("Couldn't execute query.");
$arrCounter = mysql_fetch_array($result);
$Counter = arrCounter['Counter'];
$Counter = +1;
echo $Counter;
$result = mysql_query("UPDATE Counter SET Counter='$Counter'") or die ("Couldn't execute other query.");
?>
|

05-14-2004, 06:43 PM
|
|
WebProWorld Veteran
|
|
Join Date: Jul 2003
Location: Mass, U.S.A.
Posts: 434
|
|
Being PHP eliterate:
Does
$Counter = +1;
increment $Counter? I'd guess it should be
$Counter += 1;
Beware the first line though!
K<o>
|

05-14-2004, 06:55 PM
|
|
WebProWorld Pro
|
|
Join Date: Feb 2004
Location: Colorado
Posts: 159
|
|
Good eye. :) I totally missed that.
|

05-14-2004, 07:05 PM
|
|
WebProWorld Veteran
|
|
Join Date: Jul 2003
Location: Mass, U.S.A.
Posts: 434
|
|
Quote:
|
Originally Posted by Dawson
Good eye. :) I totally missed that.
|
Huh! I don't know why I dared to write that. Lucky me it was correct. I just have programmed in so many languages, that I'm ashamed to tell people how many. So I gave it a shot.
K<o>
|

05-14-2004, 07:14 PM
|
|
WebProWorld Pro
|
|
Join Date: Feb 2004
Location: Colorado
Posts: 159
|
|
$counter++; is even easier, too. :)
|

05-15-2004, 10:03 AM
|
 |
WebProWorld Pro
|
|
Join Date: Nov 2003
Location: -_-
Posts: 187
|
|
Thanks everyone for the help :),im going to try out the scripts now
__________________
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
|- >(^_^)> | (t^^t) | <(^_^)< -|
- - - - - - - - - - - - - - - -
|

05-15-2004, 10:14 AM
|
 |
WebProWorld Pro
|
|
Join Date: Aug 2003
Location: France
Posts: 196
|
|
Conficio is right, I didn't look further that the place where you echo $counter, but the way you update it in the database is wrong also.
As when you read the $counter value, when you try to store it you just write the SQL query but you don't execute it, therefore your database will never be updated.
But Conficio made a mistake also, using 'Counter' instead of 'Counted' as your table name, therefore his code will not work anymore. Here is the right code I guess (but I did not run it) :
----
...
$result = mysql_query($query)
or die("Couldn't execute query.");
$line=mysql_fetch_array($result);
$Counter=$line['Counter'];
$Counter++; // or $Counter+=1 :)
echo($Counter);
$query= "update Counted set Counter='$Counter'";
$result=mysql_query($query) or die("Couldn't execute update.");
----
You could also save time by modifying the 'Counter' field directly in the database, without reading it as $Counter and then saving $Counter. In tha case, once connected to the database you need only 1 line of code :
---
...
$db = mysql_select_db($database,$connection)
or die ("Couldn't select database");
$result=mysql_query("update Counted set Counter=Counter+1") or die("Couldn't execute update.");
---
JP
|

05-15-2004, 09:43 PM
|
|
WebProWorld Veteran
|
|
Join Date: Jul 2003
Location: Mass, U.S.A.
Posts: 434
|
|
Quote:
|
Originally Posted by httpman
But Conficio made a mistake also, using 'Counter' instead of 'Counted' as your table name, therefore his code will not work anymore. Here is the right code I guess (but I did not run it) :
|
Hi JP,
I'm puzzeled. I thought, I did not touch the DB or any table whatsoever. I "corrected" just one variable. am I delirious?
Congratulations on the sql statement optimization (That is a language I know about). You version has the advantage to be multi thread safe and performance friendly, as it runs in one transaction. It is actually the only way to go, but I didn't want to go into that, as the original author seems to be new to programming and this subject is hard to explain.
Take care
K<o>
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|