Jump to content

Better than a newb


TheFilmGod

Recommended Posts

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

$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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.