Jump to content

A Problem With Headers


Yuval200
 Share

Recommended Posts

Hey again guys :)I have a file named get.php that does all the header commands in my system, works great.Problem is, I use the file to do some MySQL commands too, so I must include a file with the connection details ect. Now, it works great too, but a week or so ago the file made the get page to die and print some headers and outpost errors.The thing I dont get is, when I copy all the commands from the included DB file to the get file and replace the include line with those commands, it all works great.I'm desperate :)Thanks,Yuval.

Link to comment
Share on other sites

Hey again guys :)I have a file named get.php that does all the header commands in my system, works great.Problem is, I use the file to do some MySQL commands too, so I must include a file with the connection details ect. Now, it works great too, but a week or so ago the file made the get page to die and print some headers and outpost errors.The thing I dont get is, when I copy all the commands from the included DB file to the get file and replace the include line with those commands, it all works great.I'm desperate :)Thanks,Yuval.
Can you post some code so we can see what is going on. From the sound of it, it seems your getting the classic header error in which case your outputting information after the header has already been displayed. if you can post your code or a similar example of your code we might be able to see whats going on.
Link to comment
Share on other sites

Can you post some code so we can see what is going on. From the sound of it, it seems your getting the classic header error in which case your outputting information after the header has already been displayed. if you can post your code or a similar example of your code we might be able to see whats going on.
I know what error you're talking about, there is no "echo" things in the DB file. When I paste ALL the code from that file to the get.php file, it's ok, otherwise, header errors.
Link to comment
Share on other sites

It's hard to say without seeing some code.
The code doesn't matter, because when I don't include the file, but add ALL it's content, it still works..Lets say that I have 1.php and 2.php, 1.php content is:
x

2.php is:

y

When I include 1.php in 2.php, it gives headers errors, but when I copy all the code from 1.php to 2.php, like:

xy

It works perfectly..I you still don't get the errors, then I'll give the real codes for the files..Thanks.

Link to comment
Share on other sites

I'm really sorry for the late post :) Anyway, that's the code:

<?php	define(yp_mysql_connect			, mysql_connect("localhost" ,"root"))		;	define(yp_mysql_select_db		, mysql_select_db("yuvpost", yp_mysql_connect));		$form_name			= trim(mysql_real_escape_string($_POST["post_name"]))						;	$form_content		= trim(mysql_real_escape_string(nl2br(stripslashes($_POST["post_content"]))));	$form_date			= date('j/n/Y G:i')															;?>

And I tested the file and clear all the content from it, and it worked great.. I'm insane!

Link to comment
Share on other sites

I'm really sorry for the late post :) Anyway, that's the code:
<?php	define(yp_mysql_connect			, mysql_connect("localhost" ,"root"))	;	define(yp_mysql_select_db		, mysql_select_db("yuvpost", yp_mysql_connect));		$form_name			= trim(mysql_real_escape_string($_POST["post_name"]))					;	$form_content		= trim(mysql_real_escape_string(nl2br(stripslashes($_POST["post_content"]))));	$form_date			= date('j/n/Y G:i')														;?>

And I tested the file and clear all the content from it, and it worked great.. I'm insane!

Since I haven't touched PHP in a while I may be totally off-base here but according to the PHP manual, define is used to define constants (as opposed to variables/pointers/references). Maybe the problem is that you can't assign a database connection to a constant. If I'm totally wrong, just ignore this post. :)
Link to comment
Share on other sites

You are correct, sir.

Only scalar data (boolean, integer, float and string) can be contained in constants. Do not define resource constants.
The return value from mysql_connect is a resource, not a scalar type. Also, it doesn't make any sense to define a constant to be the return value of mysql_select_db, which returns true or false. So the value of the constant yp_mysql_select_db would be true or false.It doesn't really matter too much though, because you don't even need to save the return values from either mysql_connect or mysql_select_db. The only reason you would need to save the return value from mysql_connect is if you are connecting to multiple databases or database servers.Anyway, what's the problem again? You said you were including a file and it was causing a header error. Is this the file you are including? Because nothing in it would cause a header error, unless trying to define a constant resource is producing an error or a warning, which would not allow headers to be sent, but in that case I would think your question would be about the error message that is causing the headers to fail..Also, when you define a constant, the name goes in quotes. Not having the name in quotes causes a notice about an undefined constant.
Link to comment
Share on other sites

So if I got you guys right, you're saying that I can't put DB information in constants? That's interesting, because I once used a larger DB file that contained the following lines and more, and it did worked.So, you're talking about notice errors and all that stuff, can I please just have a list of all the things I need to fix? My English skills are not that strong as you might know..And yes, the error I'm getting is that "Headers allready sent" thing.Thanks guys :)

Link to comment
Share on other sites

You can't store a resource, which includes a database connection, in a constant. You can only store "scalar" or "atomic" values, which are things like numbers, words, true/false, etc. If you think about it, it makes sense because a constant, by definition, does not change. When you connect to a database to get a recordset back, that recordset may change.You can put your connection information in constants, because they are just words. You can have a constant for the database host, user, password, etc, but not the actual connection.You can replace your two define lines with just this and it will do the same thing:mysql_connect("localhost" ,"root");mysql_select_db("yuvpost");As far as the header errors, there is nothing in the code you posted that would cause it (other then possibly having an invalid connection given to the mysql_select_db function). The reason has to be somewhere else.

Link to comment
Share on other sites

"As far as the header errors, there is nothing in the code you posted that would cause it (other then possibly having an invalid connection given to the mysql_select_db function). The reason has to be somewhere else."As I fought, thanks :)Then PHP just hates me, that's logical..Thanks for the tips anyway.. I'll just put the DB info in a data file and again in the get file..Again, Thanks alot for all the help guys :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...