Fluoresce Posted September 5, 2010 Share Posted September 5, 2010 Can you see any problems with the code below? It doesn't work. What I am trying to do is quite straightforward. The links on my site look like this: go.php?id=1&url=usa go.php?id=1&url=uk go.php?id=2&url=usa go.php?id=2&url=uk When a visitor clicks on one of these links, I want to grab the values of the id and url parameters, count the click, and redirect the visitor. Here's the error that I get when I try the code: Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\sites\go.php on line 18 Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\sites\go.php:18) in C:\xampp\htdocs\sites\go.php on line 19 <?php $conn = mysql_connect('localhost','username', 'password') or trigger_error("SQL", E_USER_ERROR); mysql_select_db('database', $conn) or trigger_error("SQL", E_USER_ERROR); $id = intval($_GET['id']); $url = intval($_GET['url']); mysql_query("UPDATE urls SET click_counter = click_counter+1 WHERE id=$id"); if ($url=="usa") $href = "SELECT usa FROM urls WHERE id=$id"; elseif ($url=="uk") $href = "SELECT uk FROM urls WHERE id=$id"; elseif ($url=="aus") $href = "SELECT aus FROM urls WHERE id=$id"; elseif ($url=="can") $href = "SELECT can FROM urls WHERE id=$id"; else $href = "SELECT int FROM urls WHERE id=$id"; $qry = mysql_query($href); list($href)=mysql_fetch_row($qry); header("Location:$href"); mysql_close($conn); ?> Quote Link to comment https://forums.phpfreaks.com/topic/212590-code-not-working-want-to-count-clicks-and-redirect-visitors/ Share on other sites More sharing options...
wildteen88 Posted September 5, 2010 Share Posted September 5, 2010 Umm, seems your sql query is failing change this $qry = mysql_query($href); to $qry = mysql_query($href) or trigger_error(mysql_error()); Also you'll be better to code this if ($url=="usa") $href = "SELECT usa FROM urls WHERE id=$id"; elseif ($url=="uk") $href = "SELECT uk FROM urls WHERE id=$id"; elseif ($url=="aus") $href = "SELECT aus FROM urls WHERE id=$id"; elseif ($url=="can") $href = "SELECT can FROM urls WHERE id=$id"; else $href = "SELECT int FROM urls WHERE id=$id"; As a switch/case statement switch(strtolower($url)) { case 'uk': case 'usa': case 'aus': case 'can': $field = $url; break; case' int': default: $field = 'int'; break; } $href = "SELECT $field FROM urls WHERE id=$id"; Also this $url = intval($_GET['url']); Should be $url = $_GET['url']; Quote Link to comment https://forums.phpfreaks.com/topic/212590-code-not-working-want-to-count-clicks-and-redirect-visitors/#findComment-1107471 Share on other sites More sharing options...
Fluoresce Posted September 5, 2010 Author Share Posted September 5, 2010 Thanks, Wildteen88! It nearly works. It only fails when a link with the url parameter "int" is clicked (e.g., go.php?id=1&url=int). Here's the error message that I get: Notice: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'int FROM urls WHERE id=1' at line 1 in C:\xampp\htdocs\sites\go.php on line 21 Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\sites\go.php on line 22 Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\sites\go.php:21) in C:\xampp\htdocs\sites\go.php on line 23 Quote Link to comment https://forums.phpfreaks.com/topic/212590-code-not-working-want-to-count-clicks-and-redirect-visitors/#findComment-1107481 Share on other sites More sharing options...
Fluoresce Posted September 5, 2010 Author Share Posted September 5, 2010 There doesn't seem to be a problem with my SQL query, which is what's confusing me. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/212590-code-not-working-want-to-count-clicks-and-redirect-visitors/#findComment-1107485 Share on other sites More sharing options...
wildteen88 Posted September 5, 2010 Share Posted September 5, 2010 It is erroring out due to 'int' being a mysql reserved keyword. To prevent the error . Change $href = "SELECT $field FROM urls WHERE id=$id"; to $href = "SELECT `$field` FROM urls WHERE id=$id"; Quote Link to comment https://forums.phpfreaks.com/topic/212590-code-not-working-want-to-count-clicks-and-redirect-visitors/#findComment-1107486 Share on other sites More sharing options...
Fluoresce Posted September 5, 2010 Author Share Posted September 5, 2010 Wildteen88, you're a pro! Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/212590-code-not-working-want-to-count-clicks-and-redirect-visitors/#findComment-1107489 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.