Current location: Hot Scripts Forums » Programming Languages » PHP » Count Searches?


Count Searches?

Reply
  #1 (permalink)  
Old 09-19-04, 07:13 AM
fraggle fraggle is offline
Newbie Coder
 
Join Date: Mar 2004
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Count Searches?

I am trying to put a search count on a site. I have created a field to hold the count and then pull back result to display.
How can i increment the count each time a search is done?

All I am getting displayed now is resource id#12
My query is SELECT count FROM tablename

Thanks
Reply With Quote
  #2 (permalink)  
Old 09-19-04, 08:05 AM
Eclipse's Avatar
Eclipse Eclipse is offline
Coding Addict
 
Join Date: May 2004
Location: Long Island, New York
Posts: 356
Thanks: 0
Thanked 0 Times in 0 Posts
I don't really work with SQL but this should work anyway, set it as a varialbe for example $count then:
PHP Code:

$count $count++; 

Then write back to the db. Well hope that works for you.
Reply With Quote
  #3 (permalink)  
Old 09-19-04, 08:40 AM
hardcoded's Avatar
hardcoded hardcoded is offline
Newbie Coder
 
Join Date: Sep 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
UPDATE tablename SET count = count+1

This query assumes that your table has only one row...

EDIT: Oh, by the way Eclipse. The whole point of using the "++" operator is to avoid using assignations. Thus, it has more class to write "count++;". And though I never coded C, if I recall correctly, "count = count++" wouldn't even work, because the increment would happen AFTER the assignation. You would have to write "count = ++count;". But afterthought, I think that it doesn't matter because the ++ that happens after the assignation would make the count go up anyway. Well, all this to say that the "count = " part is completely useless.
__________________
http://www.hardcoded.net

Last edited by hardcoded; 09-19-04 at 08:48 AM.
Reply With Quote
  #4 (permalink)  
Old 09-19-04, 09:16 AM
<?Wille?> <?Wille?> is offline
Junior Code Guru
 
Join Date: Jan 2004
Location: Helsinki, Finland
Posts: 666
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by fraggle
I am trying to put a search count on a site. I have created a field to hold the count and then pull back result to display.
How can i increment the count each time a search is done?

All I am getting displayed now is resource id#12
My query is SELECT count FROM tablename

Thanks
the reson why your getting resource id#12 it that you probably just echo the query like
PHP Code:

$q mysql_query("SELECT count from tablename");

echo 
$q
if you want to do something with the values you must fetch them like:
PHP Code:

$q mysql_query("SELECT count from tablename");

while (
$r mysql_fetch_array) { # if you have only one row its possible to
echo $r['count']; # leave the while part and use only mysql_fetch_array

hope it helps
Wille
Reply With Quote
  #5 (permalink)  
Old 09-19-04, 01:35 PM
fraggle fraggle is offline
Newbie Coder
 
Join Date: Mar 2004
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Getting there

Getting closer now, I have one row to hold the count and the UPDATE is working fine, now all i get is ........

"has been searched $count times"

showing my variable name and not the result?

<?php
$count = mysql_query("SELECT count FROM stats",$dbh);
mysql_query("update stats SET count = count+1",$dbh);
?>
<?php
echo 'has been searched $count times.';
?>

It must be something simple, but Im missing it, please help...
Reply With Quote
  #6 (permalink)  
Old 09-19-04, 01:42 PM
<?Wille?> <?Wille?> is offline
Junior Code Guru
 
Join Date: Jan 2004
Location: Helsinki, Finland
Posts: 666
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by fraggle
Getting closer now, I have one row to hold the count and the UPDATE is working fine, now all i get is ........

"has been searched $count times"

showing my variable name and not the result?

<?php
$count = mysql_query("SELECT count FROM stats",$dbh);
mysql_query("update stats SET count = count+1",$dbh);
?>
<?php
echo 'has been searched $count times.';
?>

It must be something simple, but Im missing it, please help...
is this it?? or is there something your not showing us?? if this is it i doubt its updateing becourse the query is never run.. the reson why your getting "has been searched $count times." is becourse your useing single quotes even though your haveing vars inside the echo

heres how you do it:
PHP Code:

<?php

$count 
mysql_query("SELECT count FROM stats",$dbh); 
$r mysql_fetch_array($count); # assuming there is only one row in db
$q mysql_query("update stats SET count = count+1",$dbh) or die(mysql_error());

// two ways to do
# 1
echo 'has been searched '.$r['count'].' times.';
#2
echo "has been searched $r[count] times.";
?>
Reply With Quote
  #7 (permalink)  
Old 09-19-04, 01:57 PM
fraggle fraggle is offline
Newbie Coder
 
Join Date: Mar 2004
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks Willie
Reply With Quote
  #8 (permalink)  
Old 09-19-04, 01:59 PM
fraggle fraggle is offline
Newbie Coder
 
Join Date: Mar 2004
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks Willie

"is this it?? or is there something your not showing us?? ," that was it and it is updating just not displaying

I used your last posted code and it's working!! cheers
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to count mysql rows? steveo PHP 5 05-18-09 09:27 AM
Wrong parameter count for mysql_fetch_assoc() altlprsn PHP 1 05-26-04 09:40 PM
count() mivec PHP 2 04-16-04 08:52 AM
Add count to distinct name Kerjin PHP 2 04-04-04 11:25 AM
Post count, why? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 7 10-06-03 03:29 PM


All times are GMT -5. The time now is 04:30 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.