TheFilmGod Posted March 18, 2007 Share Posted March 18, 2007 How do you retrieve information from mysql? I am already connected to mysql, mysql_query("UPDATE hits SET counter=counter+1 WHERE page = '$PHP_SELF'",$db); if (mysql_affected_rows($db) < 1) { $result = mysql_query("INSERT INTO dyphits VALUES ('$PHP_SELF', 1)", $db); } mysql_close($db); ?> So what would I need to do to retrieve the information that I just updated, "counter" and then echo it? Link to comment https://forums.phpfreaks.com/topic/43267-better-than-a-newb/ Share on other sites More sharing options...
DeathStar Posted March 18, 2007 Share Posted March 18, 2007 $hits = mysql_query("SELECT `counter` FROM `hits` WHERE `page`=pagename"); // paNGE NAME IS THE PAGE YOU WANT THE HITS FOR echo "$hits"; Link to comment https://forums.phpfreaks.com/topic/43267-better-than-a-newb/#findComment-210074 Share on other sites More sharing options...
obsidian Posted March 18, 2007 Share Posted March 18, 2007 $hits = mysql_query("SELECT `counter` FROM `hits` WHERE `page`=pagename"); // paNGE NAME IS THE PAGE YOU WANT THE HITS FOR echo "$hits"; That will not give you the number of hits. That will simply return a query result. You must pull the value from the query result before you can display it: <?php $sql = mysql_query("SELECT counter FROM hits WHERE page = '$_SERVER[php_SELF]'"); $hits = mysql_result($sql, 0, 'counter'); echo $hits; ?> Link to comment https://forums.phpfreaks.com/topic/43267-better-than-a-newb/#findComment-210085 Share on other sites More sharing options...
TheFilmGod Posted March 18, 2007 Author Share Posted March 18, 2007 Hi. I have no clue what is going on. Here is the code: <?php //already connected to MYSQL mysql_query("UPDATE hits SET counter=counter+1 WHERE page = '$PHP_SELF'",$dbname); if (mysql_affected_rows($dbname) < 1) { $result = mysql_query("INSERT INTO dyphits VALUES ('$PHP_SELF', 1)", $dbname); } $sql = mysql_query("SELECT counter FROM hits WHERE page = '$_SERVER[php_SELF]'"); $hits = mysql_result($sql, 0, 'counter'); echo $hits; mysql_close($dbname); ?> These are the errors I get: Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/content/T/h/e/TheFilmGod/html/test/hits.php on line 20 Warning: mysql_affected_rows(): supplied argument is not a valid MySQL-Link resource in /home/content/T/h/e/TheFilmGod/html/test/hits.php on line 22 Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/content/T/h/e/TheFilmGod/html/test/hits.php on line 22 Warning: mysql_result(): Unable to jump to row 0 on MySQL result index 2 in /home/content/T/h/e/TheFilmGod/html/test/hits.php on line 25 Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in /home/content/T/h/e/TheFilmGod/html/test/hits.php on line 28 Link to comment https://forums.phpfreaks.com/topic/43267-better-than-a-newb/#findComment-210088 Share on other sites More sharing options...
fert Posted March 18, 2007 Share Posted March 18, 2007 change $sql = mysql_query("SELECT counter FROM hits WHERE page = '$_SERVER[php_SELF]'"); to $sql = mysql_query("SELECT counter FROM hits WHERE page = '$_SERVER[php_SELF]'") or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/43267-better-than-a-newb/#findComment-210093 Share on other sites More sharing options...
DeathStar Posted March 18, 2007 Share Posted March 18, 2007 have you included a connection to the database? Link to comment https://forums.phpfreaks.com/topic/43267-better-than-a-newb/#findComment-210106 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.