Forum Moderators: coopster
I know I have to do the redirect before any HTML is sent, even before any whitespace is sent. But even though I've got the code right at the start of the file, I still get the error message "Warning: Cannot modify header information - headers already sent..."
<?php
$connection = mysql_connect("host", "username", "password")
or die(header("Location: other_file.html"));
?>
Tom
<br />
<b>Warning</b>: mysql_connect(): Can't connect to MySQL server on 'localhost' (10061) in <b>c:\program files\apache group\apache\htdocs\index.php</b> on line <b>2</b><br />
foo
$string = "SELECT *....";if (!$query = @mysql_query($string) )
{
header ( "Location: blah.php" );
exit;
}
[EDIT]
Btw, if you want to know why it's like this, die() is basically just an echo. It stops the page execution and then sends headers so that it can write the message you want to "die" with. Therefore, you're trying to change the headers after they've already been sent.
But it doesn't give me a reference to the connection that I can use to close it later, so is it OK to connect twice like this?
<?php
// Test connection
if (!@mysql_connect("localhost", "user", "pass") )
{
header ( "Location: file.php" );
exit;
}
// Connect
$connection = mysql_connect("localhost", "user", "pass") or die();
?>
So that later on I can do this:
<?php
mysql_close($connection);
?>
$query = "SELECT test FROM test2 WHERE 1=1;";
$result = mysql_query($query);
^^^ This should work.
SO, you don't need to have your ressource #...
Howerver, try something like this:
if(!$connection=@mysql_connect(<...>)) {
die("header("location:<...>"));
}
$query = "<...>";
$result = mysql_query($query,$connection);
Hope this help.