Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stats Record

Status
Not open for further replies.

bentleykf

Programmer
Apr 29, 2002
59
AU
Can somebody tell me whats wrong with this script. It keeps on returning the value 1 for $count when it should be adding 1 each time the page is loaded.

------------------------------
<?php
if(!$search) {
$search = &quot;default&quot;;}
$dbthree = mysql_connect(&quot;localhost&quot;, &quot;username&quot;,&quot;password&quot;);
@mysql_select_db(&quot;database_db&quot;,$dbthree);
$result = mysql_query(&quot;SELECT FROM counter where name='$search'&quot;,$dbthree);

if(mysql_numrows($result)==&quot;1&quot;) {
$row = mysql_fetch_array($result);
$number = $row[&quot;count&quot;];
$start = $row[&quot;start&quot;];
$count = $number++;
$ret = mysql_query(&quot;UPDATE counter SET count='$count' WHERE name='$search'&quot;);
print(&quot;<font class=contentbl>This search query searched $count time(s)</font>&quot;);
} else {
$count = &quot;1&quot;;
$ret=mysql_query(&quot;INSERT INTO counter (start,count,name) VALUES (current_date,'$count','$search')&quot;);
print(&quot;<font class=contentbl>This search query searched $count time(s)</font>&quot;);
}
mysql_close($dbthree);
 
Try

$count = ++$number;

instead of

$count = $number++;


In your script $count will be set equal to $number and after that $number will be increased by one. So $count will always be the same as $number. You should increment first before assigning the value to $count (pre-increment instead of post-increment).

It's not a lie if YOU believe it ;-)
 
nup, doesn't seem to work. I'm thinking that it may be a problem with the if statement at the front.
 
hello

try
mysql_num_rows() instead of mysql_numrows()
 
nah, i have decided to get rid of the statement that checks if there is a $result value, rather i changed it to check if there is not a $result value, like below;

----------------------------
while ($row = mysql_fetch_array($query)) {
$start = $row[&quot;start&quot;];
$count = $row[&quot;count&quot;];
}
if(!$count){
mysql_query(&quot;INSERT INTO counter (start,count,name) VALUES (current_date()+0,\&quot;1\&quot;,\&quot;$search\&quot;)&quot;);
$count = &quot;1&quot;;
}
printf(&quot;<font class=contentbl>This search query searched $count time(s)</font>&quot;);

++$count;
mysql_query(&quot;UPDATE counter SET count=\&quot;$count\&quot; WHERE name=\&quot;$search\&quot;&quot;);
----------------------------

thanks for the help dr genius & spookie
-bentley k. frog
 
I've got a new problem now. I want to display this database on the same page, but I only want to display the top ten records of the database in descending order, according to the count feild. I've tried stuff like using this script in the mysql_query string;
$resulttwo = mysql_query(&quot;SELECT * FROM counter ORDER by count LIMIT 10&quot;);
but all that seems to do is choose 10 different records (not nessessarily the top ten records) and order them in descending order.
-bentley k. frog
 
don't worry about the previous message, I figured it out.
I replaced the mysql_query statement with this;
mysql_query(&quot;SELECT * FROM counter ORDER BY count DESC LIMIT 0,10&quot;);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top