MP145 Posted January 10, 2009 Share Posted January 10, 2009 Hi guys, I have this simple page view counter in php that stores the page name into mysql table and displays the no of times the page has been viewed. When the page is called the first time, i get this Notice: Undefined variable: vpage in /page path/ on line 107 but the initial line of code for the if result==0 insert value works, but it's not displaying. Is it because of the while loop? And when i refresh the page or call it the second time it displays the data. How can i rectify this? and do let me know if there are better or simpler way to code this. <?php $pagename = $_SERVER["REQUEST_URI"]; $parts = Explode('/', $pagename); $npage = $parts[count($parts) - 1]; mysql_connect($dbhost,$dbuser,$dbpass) or die("Error:: can't connect to database".mysql_error()); $db5 = mysql_select_db("$dbname") or die("Unable to select db"); $result=mysql_query("Select * from pageview where namapage='$npage'") or die(mysql_error()); if (mysql_num_rows($result)==0){ mysql_query("insert into pageview (namapage,hits) values ('$npage','1')"); } while($pinfo = mysql_fetch_array( $result )) { $count=$pinfo['hits']; $count=$count + 1; mysql_query ("update pageview set hits='$count' where namapage='$npage'"); $vpage=$count; // will echo this on the designated area (Line 107) } ?> Thanks. Quote Link to comment Share on other sites More sharing options...
MP145 Posted January 11, 2009 Author Share Posted January 11, 2009 ok, sorry. It was my mistake..it was the while loop after all. Changed it to : <?php $pagename = $_SERVER["REQUEST_URI"]; $parts = Explode('/', $pagename); $npage = $parts[count($parts) - 1]; mysql_connect($dbhost,$dbuser,$dbpass) or die("Error:: can't connect to database".mysql_error()); $db5 = mysql_select_db("$dbname") or die("Unable to select db"); $result=mysql_query("Select * from pageview where namapage='$npage'") or die(mysql_error()); if (mysql_num_rows($result)==0){ mysql_query("insert into pageview (namapage,hits) values ('$npage','1')"); } $pinfo = mysql_fetch_array( $result ); $count=$pinfo['hits']; $count=$count + 1; mysql_query ("update pageview set hits='$count' where namapage='$npage'"); $vpage=$count; ?> Quote Link to comment Share on other sites More sharing options...
SammyP Posted January 11, 2009 Share Posted January 11, 2009 What is happening is that you query your database to find out if the page exists, and if it doesn't you add it. All well and good. However then you go on to use the results of the initial query, which returned no records. It doesn't matter that you have subsequently added a record, as the initial result set is unaffected. You could requery the database, but you already know what it will say. So, inside your loop where you add the record, just set $count to equal 1. Then, only run the second query if the insert part did not run, so use an else clause on your initial if statement. Inside the else clause, get your $count value, increment it, and run your update statement. You can skip the while loop altogether I think, as you're only expecting one row to be returned, and at that stage you will already know that there is at least one row. In pseudo code: if (count of rows = 0) { insert a row set $count = 1 } else { get $count $count++ update database with new count value } use $count I'm not sure why the vpage thing is erroring. I don't understand exactly what you're saying there. Quote Link to comment Share on other sites More sharing options...
SammyP Posted January 11, 2009 Share Posted January 11, 2009 And I'd also not put quotes around the numbers in your SQL statements, unless the field is stored as text for some reason. It might work, but it shouldn't really. Quote Link to comment Share on other sites More sharing options...
MP145 Posted January 11, 2009 Author Share Posted January 11, 2009 Thanks SammyP for the reply. I made some changes and it's all good. Thanks for pointing out the What is happening is that you query your database to find out if the page exists, and if it doesn't you add it. All well and good. However then you go on to use the results of the initial query, which returned no records. It doesn't matter that you have subsequently added a record, as the initial result set is unaffected. <?php $pagename = $_SERVER["REQUEST_URI"]; $parts = Explode('/', $pagename); $npage = $parts[count($parts) - 1]; mysql_connect($dbhost,$dbuser,$dbpass) or die("Error:: can't connect to database".mysql_error()); $db5 = mysql_select_db("$dbname") or die("Unable to select db"); $result=mysql_query("Select * from pageview where namapage='$npage'") or die(mysql_error()); if (mysql_num_rows($result)==0){ mysql_query("insert into pageview (namapage,hits) values ('$npage','1')"); } $result1=mysql_query("Select * from pageview where namapage='$npage'") or die(mysql_error()); $pinfo = mysql_fetch_array( $result1 ); $count=$pinfo['hits']; $count=$count + 1; mysql_query ("update pageview set hits=$count where namapage='$npage'"); $vpage=$count; ?> Quote Link to comment 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.