Contact Us Forum Rules Search Archive
WebProWorld Part of WebProNews.com
Page One Link To Us Edit Profile Private Messages Archives FAQ RSS Feeds  
 

Go Back   WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum
Subscribe to the Newsletter FREE!


Register FAQ Members List Calendar Arcade Chatbox Mark Forums Read

Web Programming Discussion Forum Working with an API? Developing a plugin? Writing a Mod or script for your favorite blog, Web 2.0 site or Forum? Welcome.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-13-2004, 02:36 PM
labrynth_of_fire's Avatar
WebProWorld Pro
 

Join Date: Nov 2003
Location: -_-
Posts: 187
labrynth_of_fire RepRank 0
Default 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) | <(^_^)< -|
- - - - - - - - - - - - - - - -
Reply With Quote
  #2 (permalink)  
Old 05-13-2004, 06:16 PM
splinter's Avatar
WebProWorld Pro
 

Join Date: Jul 2003
Location: UK
Posts: 261
splinter RepRank 0
Default

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.
Reply With Quote
  #3 (permalink)  
Old 05-14-2004, 02:24 PM
mushroom's Avatar
WebProWorld Veteran
 

Join Date: Feb 2004
Location: Queen Charlotte B. C. Canada
Posts: 351
mushroom RepRank 0
Default

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
Reply With Quote
  #4 (permalink)  
Old 05-14-2004, 02:32 PM
mushroom's Avatar
WebProWorld Veteran
 

Join Date: Feb 2004
Location: Queen Charlotte B. C. Canada
Posts: 351
mushroom RepRank 0
Default

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
Reply With Quote
  #5 (permalink)  
Old 05-14-2004, 02:41 PM
httpman's Avatar
WebProWorld Pro
 

Join Date: Aug 2003
Location: France
Posts: 196
httpman RepRank 0
Default

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
__________________
www.net-createurs.com [ french only website sorry ! ]
Reply With Quote
  #6 (permalink)  
Old 05-14-2004, 05:57 PM
WebProWorld Pro
 

Join Date: Feb 2004
Location: Colorado
Posts: 159
Dawson RepRank 0
Default

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.");
?>
Reply With Quote
  #7 (permalink)  
Old 05-14-2004, 06:43 PM
WebProWorld Veteran
 

Join Date: Jul 2003
Location: Mass, U.S.A.
Posts: 434
Conficio RepRank 0
Default

Being PHP eliterate:

Does

$Counter = +1;

increment $Counter? I'd guess it should be

$Counter += 1;

Beware the first line though!

K<o>
Reply With Quote
  #8 (permalink)  
Old 05-14-2004, 06:55 PM
WebProWorld Pro
 

Join Date: Feb 2004
Location: Colorado
Posts: 159
Dawson RepRank 0
Default

Good eye. :) I totally missed that.
Reply With Quote
  #9 (permalink)  
Old 05-14-2004, 07:05 PM
WebProWorld Veteran
 

Join Date: Jul 2003
Location: Mass, U.S.A.
Posts: 434
Conficio RepRank 0
Default

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>
Reply With Quote
  #10 (permalink)  
Old 05-14-2004, 07:14 PM
WebProWorld Pro
 

Join Date: Feb 2004
Location: Colorado
Posts: 159
Dawson RepRank 0
Default

$counter++; is even easier, too. :)
Reply With Quote
  #11 (permalink)  
Old 05-15-2004, 10:03 AM
labrynth_of_fire's Avatar
WebProWorld Pro
 

Join Date: Nov 2003
Location: -_-
Posts: 187
labrynth_of_fire RepRank 0
Default

Thanks everyone for the help :),im going to try out the scripts now
__________________
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
|- >(^_^)> | (t^^t) | <(^_^)< -|
- - - - - - - - - - - - - - - -
Reply With Quote
  #12 (permalink)  
Old 05-15-2004, 10:14 AM
httpman's Avatar
WebProWorld Pro
 

Join Date: Aug 2003
Location: France
Posts: 196
httpman RepRank 0
Default

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
__________________
www.net-createurs.com [ french only website sorry ! ]
Reply With Quote
  #13 (permalink)  
Old 05-15-2004, 09:43 PM
WebProWorld Veteran
 

Join Date: Jul 2003
Location: Mass, U.S.A.
Posts: 434
Conficio RepRank 0
Default

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>
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum
Tags: , ,



Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Search Engine Optimization by vBSEO 3.2.0