djcochran Posted April 10, 2009 Share Posted April 10, 2009 I'm trying to use read data from a table and increment the visitor counter on my site, but I'm having a little trouble. My mysql table is like this (data is int(11))... -counters- count - 0 cnum - 10000 onum - 1000 I'm trying to increment count each time someone visits the site. I've tried several different approaches that I've found online, but I can't get anything to work. <?php // Connects to your Database mysql_connect("localhost", "xxxxx", "xxxx") or die(mysql_error()); mysql_select_db("student5") or die(mysql_error()); //Adds one to the counter mysql_query("UPDATE counters SET count = count + 1"); //Retreives the current count $count = mysql_fetch_row(mysql_query("SELECT count FROM counters")); //Displays the count on your site print "$count[0]"; ?> This gives me the error Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /export/home/student5/html/countprac.php on line 10. I've tried several different codes and got similar results. Any tips? ??? Quote Link to comment https://forums.phpfreaks.com/topic/153490-solved-phpmysql-simple-hit-counter-help/ Share on other sites More sharing options...
gaza165 Posted April 10, 2009 Share Posted April 10, 2009 $count = mysql_fetch_row(mysql_query("SELECT count FROM counters")); change the above line to... $query = mysql_query("SELECT count FROM counters"); $count = mysql_fetch_row($query); let us know if htat helps... Quote Link to comment https://forums.phpfreaks.com/topic/153490-solved-phpmysql-simple-hit-counter-help/#findComment-806471 Share on other sites More sharing options...
PFMaBiSmAd Posted April 10, 2009 Share Posted April 10, 2009 Your mysql_query() statements don't have any error checking, so no one can really tell you why they are failing. And don't nest function calls - mysql_fetch_row(mysql_query("SELECT count FROM counters")) because it makes it impossible to do any error checking, error reporting, or error recovery in your code. Change your first query to this - mysql_query("UPDATE counters SET count = count + 1") or die("Update query failed: " . mysql_error()); Change your second query to this - $query = "SELECT count FROM counters"; $result = mysql_query($query) or die("Select query failed: " . mysql_error()); $count = mysql_fetch_row($result); Quote Link to comment https://forums.phpfreaks.com/topic/153490-solved-phpmysql-simple-hit-counter-help/#findComment-806473 Share on other sites More sharing options...
djcochran Posted April 10, 2009 Author Share Posted April 10, 2009 Okay. I made the changes that you gave, gaza, and tried it out. I then got this error... Update query failed: Unknown column 'count' in 'field list'. I also got this message a few times with other code, and I couldn't figure out what to do. Quote Link to comment https://forums.phpfreaks.com/topic/153490-solved-phpmysql-simple-hit-counter-help/#findComment-806527 Share on other sites More sharing options...
PFMaBiSmAd Posted April 10, 2009 Share Posted April 10, 2009 There is no column named count in the table you are using. Quote Link to comment https://forums.phpfreaks.com/topic/153490-solved-phpmysql-simple-hit-counter-help/#findComment-806538 Share on other sites More sharing options...
djcochran Posted April 10, 2009 Author Share Posted April 10, 2009 I'm sorry, I made the corrections that PFMaBiSmAd suggested earlier... Where am I getting the error about count not being a column. How do I read from that table then? Quote Link to comment https://forums.phpfreaks.com/topic/153490-solved-phpmysql-simple-hit-counter-help/#findComment-806549 Share on other sites More sharing options...
PFMaBiSmAd Posted April 10, 2009 Share Posted April 10, 2009 Cannot help you with that question. Its your table, you are the one who knows what columns are in it or if your count column is in a different table you should know the correct table to use. Quote Link to comment https://forums.phpfreaks.com/topic/153490-solved-phpmysql-simple-hit-counter-help/#findComment-806557 Share on other sites More sharing options...
djcochran Posted April 10, 2009 Author Share Posted April 10, 2009 Waaaait...I'm looking at it right now and it lists it kind of like this... name | num --------------- count - 0 cnum - 10000 onum - 1000 Is name the column I'm looking for? Or num (since it's the data)? I just thought use and select were to select the table and the specific row (like count and cnum). Quote Link to comment https://forums.phpfreaks.com/topic/153490-solved-phpmysql-simple-hit-counter-help/#findComment-806565 Share on other sites More sharing options...
laffin Posted April 10, 2009 Share Posted April 10, 2009 Yes and wut exacly are you trying to do. MySQL needs to know which record you are refferring to and how. but if your trying to grab all records into an array. than you will have to loop $count = mysql_fetch_row(mysql_query("SELECT num FROM counters WHERE name='count'")); so you have to follow what yer field names are here we just tell it, we want field 'num' from counters WHERE name is 'count' Quote Link to comment https://forums.phpfreaks.com/topic/153490-solved-phpmysql-simple-hit-counter-help/#findComment-806577 Share on other sites More sharing options...
djcochran Posted April 10, 2009 Author Share Posted April 10, 2009 I am simply trying to create a hit counter to place on my website. I initialized count to zero, and I want to be able to update that each time some visits the site (by one, of course) and display the current data from count in the counters table. Quote Link to comment https://forums.phpfreaks.com/topic/153490-solved-phpmysql-simple-hit-counter-help/#findComment-806589 Share on other sites More sharing options...
laffin Posted April 10, 2009 Share Posted April 10, 2009 I was making a statement, yer SQL statement was wrong. as it was referencing the contents of the fields, instead of referencing the fields themselves. name | num --------------- count - 0 cnum - 10000 onum - 1000 The field names are the headers in this table. Not the contents. look at the sql I provided above. That shud do wut u want. Quote Link to comment https://forums.phpfreaks.com/topic/153490-solved-phpmysql-simple-hit-counter-help/#findComment-806594 Share on other sites More sharing options...
djcochran Posted April 10, 2009 Author Share Posted April 10, 2009 Well...this is what my code looks like now, but I'm still getting the same "Update query failed: Unknown column 'count' in 'field list'." <?php // Connects to your Database mysql_connect("localhost", "xxxx", "xxxx") or die(mysql_error()); mysql_select_db("student5") or die(mysql_error()); //Adds one to the counter mysql_query("UPDATE counters SET count = count + 1") or die("Update query failed: " . mysql_error()); //Retreives the current count $query = "SELECT count FROM counters"; $result = mysql_query($query) or die("Select query failed: " . mysql_error()); $count = mysql_fetch_row(mysql_query("SELECT num FROM counters WHERE name='count'")); //Displays the count on your site print "$count[0]"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/153490-solved-phpmysql-simple-hit-counter-help/#findComment-806596 Share on other sites More sharing options...
laffin Posted April 10, 2009 Share Posted April 10, 2009 mysql_query("UPDATE counters SET num = num + 1 WHERE name='count'") look at the previous queries they all reference the content again, not the field names. you have 2 field names, name and num. not count, onum, cnum... these are the contents of the field name so u have to tell MySQL what field yer updating and which row. The field yer updating is 'num' where the row has field 'name' contains 'count' Again yer table has no 'count' field, this is where yer misunderstanding go through yer code again, and look if you can fix the rest its easy after ya get the hang of it Quote Link to comment https://forums.phpfreaks.com/topic/153490-solved-phpmysql-simple-hit-counter-help/#findComment-806605 Share on other sites More sharing options...
djcochran Posted April 10, 2009 Author Share Posted April 10, 2009 Oh oh ohhhh...Now I understand! The names are simply data, just like the 0, 10000, and 1000. That makes sense. I don't know why I was confusing them that way. Thanks for all the help! Quote Link to comment https://forums.phpfreaks.com/topic/153490-solved-phpmysql-simple-hit-counter-help/#findComment-806611 Share on other sites More sharing options...
laffin Posted April 10, 2009 Share Posted April 10, 2009 Glad you caugt it now Congratz Quote Link to comment https://forums.phpfreaks.com/topic/153490-solved-phpmysql-simple-hit-counter-help/#findComment-806661 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.