Page 1 of 1

PHP Redirect Problem

Posted: Mon Apr 21, 2008 12:43 pm
by JSH1
Hi Everyone,
I am a PHP newbie and am doing something that looks really easy, but it won't work and I am stumped.
I have a MySQL database with fields named mid and mlink.

mid is a numeric value that is unique to each record.

mlink is a url associated with the record and is also a unique value.

Below is the script I am trying to use. What I am trying to accomplish is to write a URL like http://www.mydomain.com/out.php?id=4 and have that redirect the user to the URL that is present in the URL field in the db. I know I have the db and the table defined properly, but when I try to make the link to fire the redirection, I get this message. (I am running php5)
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 ''xxxx')' at line 1

Code: Select all

<?php
require("myconfigurationscript.php");
dbinit();
$sitecode="xxxx";
 
 
$linkid=$_GET['mid'];
$visitorip= $_SERVER['REMOTE_ADDR'];
$visitorurl= $_SERVER['HTTP_REFERER'];
$visitorbrowser= $_SERVER['HTTP_USER_AGENT'];
$visitorpage=$_SERVER['REQUEST_URI'];
 
 
$sqlredir="SELECT * FROM tablename WHERE mid=$linkid";
$rdresult=mysql_query($sqlredir) or die(mysql_error());
$row=mysql_fetch_assoc($rdresult) or die(mysql_error());
$rdurl="Location: ".$row['mlink'];
header($rdurl);
 
 
mysql_free_result($rdresult);
mysql_free_result($result);
$goodbye=mysql_close($conn);
 
?>
Thank you in advance for any help you can offer.

Scott

Re: PHP Redirect Problem

Posted: Mon Apr 21, 2008 1:59 pm
by flying_circus
It's dying and printing the SQL error, as it should, at line 15.

What are you using to manage your MySQL Database? Are you using MySQL's admin tools or PHPMyAdmin?

Try printing the query string and then manually running it on your SQL server. If your table name is spelled wrong, the field is spelled wrong, or perhaps $linkid is null or an innapropriate data type.

Code: Select all

<?php
require("myconfigurationscript.php");
dbinit();
$sitecode="xxxx";
 
 
$linkid=$_GET['mid'];
$visitorip= $_SERVER['REMOTE_ADDR'];
$visitorurl= $_SERVER['HTTP_REFERER'];
$visitorbrowser= $_SERVER['HTTP_USER_AGENT'];
$visitorpage=$_SERVER['REQUEST_URI'];
 
print "SELECT * FROM `tablename` WHERE `mid`='$linkid'";
 
/* 
$sqlredir="SELECT * FROM tablename WHERE mid=$linkid";
$rdresult=mysql_query($sqlredir) or die(mysql_error());
$row=mysql_fetch_assoc($rdresult) or die(mysql_error());
$rdurl="Location: ".$row['mlink'];
header($rdurl);
 
 
mysql_free_result($rdresult);
mysql_free_result($result);
$goodbye=mysql_close($conn);
*/
?>
A couple of tips: ALWAYS encapsulate values in single quotes ('). It's also a good practice to use back ticks (`) around table and field names.

Re: PHP Redirect Problem

Posted: Mon Apr 21, 2008 4:03 pm
by JSH1
I am using phpmyadmin.
All field names and table names are spelled correctly and are all in lower case. The mid field in an integer field and the mlink is a varchar field with a max length of 255 set. Max length is certainly no problem.
The database works beautifully on the site. The site is a few years old and has had no problems. I had is developed for me. I am not much of a programmer.
I am just a little baffled at why this won't work.

Re: PHP Redirect Problem

Posted: Mon Apr 21, 2008 4:10 pm
by John Cartwright
what did the snipplet above output?