PHP & MySQL
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.
Related Article: php array to mysql tabel
is a PHP discussion thread by sowh4t that has 1 reply, was last updated 1 year ago and has been tagged with the keywords: php, array, to, mysql, tabel.
Djmann1013
Junior Poster
104 posts since May 2012
Reputation Points: 0
Solved Threads: 3
Skill Endorsements: 0
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.. :)
phorce
Master Poster
738 posts since Jul 2011
Reputation Points: 63
Solved Threads: 91
Skill Endorsements: 16
I want it to rediect to the URL that is stored in the MySQL table.
Djmann1013
Junior Poster
104 posts since May 2012
Reputation Points: 0
Solved Threads: 3
Skill Endorsements: 0
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';
}
?>
phorce
Master Poster
738 posts since Jul 2011
Reputation Points: 63
Solved Threads: 91
Skill Endorsements: 16
Djmann1013
Junior Poster
104 posts since May 2012
Reputation Points: 0
Solved Threads: 3
Skill Endorsements: 0
It continues to give me a 404 error.
Djmann1013
Junior Poster
104 posts since May 2012
Reputation Points: 0
Solved Threads: 3
Skill Endorsements: 0
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!!!
phorce
Master Poster
738 posts since Jul 2011
Reputation Points: 63
Solved Threads: 91
Skill Endorsements: 16
I'll have to try it later...My phpmyadmin is giving me false errors....
Djmann1013
Junior Poster
104 posts since May 2012
Reputation Points: 0
Solved Threads: 3
Skill Endorsements: 0
Okay, let me know how it goes :)!
phorce
Master Poster
738 posts since Jul 2011
Reputation Points: 63
Solved Threads: 91
Skill Endorsements: 16
I got "Could Not Fetch URL"
Djmann1013
Junior Poster
104 posts since May 2012
Reputation Points: 0
Solved Threads: 3
Skill Endorsements: 0
Question Answered as of 9 Months Ago by
phorce