Hi I am having trouble with this code:

<?

$host="mysql6.000webhost.com"; // Host name 
$username="removed"; // Mysql username 
$password="removed"; // Mysql password 
$db_name="removed"; // Database name 
$tbl_name="direct"; // Table name


// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("Error. MySQL Not Responding.");

$SQL = "SELECT * FROM $tbl_name";
$result = mysql_query($SQL);

while ($db_field = mysql_fetch_assoc($result)) {
$url = $db_field['url'];
}
//Banned st00f...
include('/home/a1092592/public_html/ban/banned.php');
//redirect....
   header("location:$url");

?>

This code is not working properly. I have a MySQL Database that holds the URL in the $url variable. It pulls it straight off the table. But it returns a 404 error, even though the URL is alive. I tried google.com, and it errored out with a 404. But, I tried storing the URL in a string, and it worked. But I want it to work with the MySQL Database.

Hey first off, for your own purpose, please don't post your MYSQL Account information on a public forum that anyone can see ;)

Second, can you please just var_dump the $url variable where you're trying to re-direct.. So for example:

db_field

<?php

$host="mysql6.000webhost.com"; // Host name 
$username="removed"; // Mysql username 
$password="removed"; // Mysql password 
$db_name="removed"; // Database name 
$tbl_name="direct"; // Table name


// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("Error. MySQL Not Responding.");

$SQL = "SELECT * FROM $tbl_name";
$result = mysql_query($SQL);

while ($db_field = mysql_fetch_assoc($result)) {
$url = $db_field['url'];
}

var_dump($url);
//Banned st00f...
//include('/home/a1092592/public_html/ban/banned.php');
//redirect....
 //  header("location:$url");

?>

Then post the the result.. :)

I want it to rediect to the URL that is stored in the MySQL table.

Try this:

<?php

$host="mysql6.000webhost.com"; // Host name 
$username="removed"; // Mysql username 
$password="removed"; // Mysql password 
$db_name="removed"; // Database name 
$tbl_name="direct"; // Table name


// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("Error. MySQL Not Responding.");

$SQL = "SELECT url FROM $tbl_name";
$result = mysql_query($SQL);

if(mysql_affected_rows() == 1) // assuming there is only ONE url inside the table
{
    $row = mysql_fetch_row($result);
    $url = $row[0];
    header("Location: $url");
}else{
  echo 'Cannot fetch the URL';  

}
?>
commented: Thank you for helping me. I was literally tearing my hair out. +1

It continues to give me a 404 error.

Ok.

Can you please tell me what the result of this is:

<?php

$host="mysql6.000webhost.com"; // Host name 
$username="removed"; // Mysql username 
$password="removed"; // Mysql password 
$db_name="removed"; // Database name 
$tbl_name="direct"; // Table name


// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("Error. MySQL Not Responding.");

$SQL = "SELECT url FROM $tbl_name";
$result = mysql_query($SQL);

if(mysql_affected_rows() == 1) // assuming there is only ONE url inside the table
{
    $row = mysql_fetch_row($result);
    $url = $row[0];
    var_dump($url);
    //header("Location: $url");
}else{
  echo 'Cannot fetch the URL';  

}
?>

It will give either NULL, or a string value.. Please post this!!!

I'll have to try it later...My phpmyadmin is giving me false errors....

Okay, let me know how it goes :)!

I got "Could Not Fetch URL"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.