PDA

View Full Version : useing MySQL to redirect


piniyini
10-18-2002, 05:57 PM
Hello guys,

I've been trying to redirect using MySQL but I havnt been successful. Also, I'm self taught (meaning I dont know much).

This is what I'm trying to do.
I've got a file called "click.php", and i want it to redirect people by adding a querystring to it. So if I have click.php?id=2 it would go to, say, hotmail.com.
I've got a table called links, inside it I've got 2 fields, 1 called id, the other called to. I am trying to redirect depending on the id.

I got a code from a webpage (which was used for something else) and modified it, but it doesnt work.

Help ! Here's the code :

<?php header('Cache-Control:no-cache,no-store,must-revalidate');header('Pragma:no-cache');

$host = "hostname";
$user = "user";
$pass = "pass";
$database = "thewizsite_uk_db";
$table = "links";

// Connecting to the Database
$connect = @mysql_connect($host, $user, $pass) or die("could not connect to server");

// Selecting the Database for use
$db_select = @mysql_select_db($database) or die("could not select the database");

// Compose SQL command
$SQL = "SELECT to
FROM $table
WHERE (id='$id')
";

// Query the Database
$result = @mysql_query($SQL) or die("could not complete your query");

// Loop the results
// Note, results are returned as an
// Array - we cannot just print it out.
while($row = @mysql_fetch_array($result)) {
$to = $row["to"];
header('location:$to');
} ;
exit; ?>

I've been trying different stuff for ages, so any help will be much appreciated !

Ökii
10-18-2002, 06:53 PM
A quick scan through ....

Single quotes in php output will NOT attribute values to vars, only double quotes will

$var = 'value';
echo '$var'; ------------> $var
echo "$var"; -----------> value

So the header('location:$var') wouldn't attribute the value of $var in the header output. Just use double quotes there instead.

header("location: $to");

should do the trick if your query is working properly.

piniyini
10-18-2002, 07:57 PM
Nah, that doesnt work.

Anyone else care to help?

piniyini
10-19-2002, 10:54 AM
I've got the code, I was messing around with it and it worked. Dont understand a word of it though.

Thanks all the same guys.

Bye.