Nothing works! What to do???


Quick recommendations.
1. Make sure you see error messages if they occur.
For this you need to add 2 lines to the beginning of the script
ini_set ( 'display_errors' , 1 );
error_reporting ( E_ALL );
Although in some cases this still does not help. Then see the errors in the logs of the web server.
You can also add a line to the .htaccess file
php_flag display_errors 1 Be sure to remove all dogs (@) from the code!
If the apache issues an error of 500 - it means you have to look at the error text in the error log of the web server.

2. If you have problems with MySQL (under the line that caused the error, mysql_error() and the query itself - for visual control and copying to the forum. I repeat - you need to withdraw the request! And not the PHP code that forms it.

3. When working with images , to see the error message, you must guess to disable the output of the header telling the browser that the picture goes next.
And, of course, refer to the script directly, and not through the <img> tag!

4. If you have problems in the game, first of all look at the array $ _FILES ( print_r ( $_FILES ); print_r ( $_FILES ); ). Descriptions of errors from $ _FILES ['filename'] ['error'] is in the manual.

5. If there are problems in the interaction of the server and the client (cookies, sessions, requests) - it is mandatory to watch the exchange of HTTP headers

6. AND THE MOST IMPORTANT: when launching a script, look not at what the browser shows, but INSERT HTML code! .

Having received the error message, you can read it and fix it.
If you do not manage - write to the forum. At the same time, COPY an error message, and COPY a small - 3-5 lines - a piece of code, to which the error indicates. I repeat - COPY! No gagging!

If you still did not find the error - read on:

Introduction. Very important.
You wrote the program, but it does not work.
You see a few options - either sit and try to find the error with mental effort, for the hundredth time looking through the code, or go to the forum and ask that you find an error there.
The most interesting thing is that there is a third, a hundred times better than the first two.
This method is called "Debugging a program." In English - debug.
It consists in making the program itself show where it is a mistake.
This is not enough that it will be faster than asking outside - this is often the only way to solve the problem . Only.
I'll open a terrible secret for you right now. In the world there are no programmers who write code, like artists on the Arbat - sat, navayal, gave. Nope. Also will not be.
The process of writing a program is cyclic: I wrote a piece of code - I looked at how it works. If it does not work, look for errors. Works - we write further.
The only way. There are no other options.
Furthermore. In most cases, it's completely useless to throw out your code on the forum, and ask - "What's the mistake?" . The forum does not sit wizards mixed with telepaths. And fortune-tellers with soothsayers - also not. So guess what, theoretically, there can be a mistake, no one will. The error can only be found by the program owner. On your server. With its settings and misspellings. Therefore, it is possible to localize the error - to find the place where it occurs, to determine the type of error - only independently. But to fix it on the forum will help. If you do not get it yourself.

Those who come to web programming from design, or from games, or from nothing to do, just do not know this terrible secret: The main time a programmer is not going to write code. The main programmer's time is spent looking for errors and debugging. I'm not kidding. It's true. And if you decide to do programming, then you will have to look for mistakes in the same way as everyone else does.
Unfortunately, a lot of people come to PHP without any programming experience at all and, as a result, have never heard of debugging.
And this is the most important thing in programming - the ability to look for errors.
And we will study with them now.

The program is not working. What can be done in this case?

Error messages about PHP.
Your very big helper in debugging is PHP itself. If you have any problems, he will tell you about them. That is, first of all you have to make sure that if there is an error message, you will see it.

Explanation: A lot of people do not understand what an error message is. Basically these people are divided into two categories. The first one believes that error messages are a whim of the language developers, an annoying service made to ensure that the programmer is not bored. And get rid of messages by any available means. The second category did not grow out of school age and perceives not the sense of error, but only the very fact. They take php for bitch drunkenness, who scolds not in the case, but abstractly, because they are dumb. That is, the fact of appearance of an error causes these people only negative emotions, and in sense they do not even try to get a grasp.

These are monstrous delusions. Error messages are HELP! This is a huge help to the programmer. How to use it, we will consider below.

Even the most working code, which you 100% trust, and which works on the next machine, like a clock, can produce an error message. The reasons for this can be an infinite number. This and such a common case, as the lack of access rights to files, and such exotic as the prohibition by the provider of the most common and innocuous functions.

Your task is to provide the interpreter with the opportunity to inform you about an error when it occurs.
You should be sure that if the execution of the program causes an error, then you will see this error.
That the interpreter is not forbidden to tell you about the error.
That you have requested all options for displaying the error message.

First, you need to find out whether errors are displayed on the screen or written to the log. Usually, a home or test server is configured so that errors are displayed. The working server, with the site in public access, MUST be configured so that errors are not displayed (because they will not say anything to the visitor, but the programmer will not see them), but they are written to the log, where the programmer will see them.
If you are not sure, and do not know where to look, and you have to find the error urgently, write two lines at the very beginning of the script
ini_set ( 'display_errors' , 1 );
error_reporting ( E_ALL ^ E_NOTICE );
These two lines cause you to display messages about all critical errors on the screen.
If you do not get any errors, you need to write
error_reporting ( E_ALL ); This will help a lot , showing non-existent variables and other minor errors, which are usually ignored, but on which the program's performance may depend.
IMPORTANT! In the case of a syntax error, for obvious reasons, the installation with ini_set does not work.
Therefore, it is better not to rely on it, but either fix it in php.ini (or in .haccess add the line php_flag display_errors 1 ), or look for an error in the log.

Second, make sure that the code does not contain the '@' characters before the function names. This disables the output of the error message. Pretty business! You're looking for a mistake-looking for a thing, and shut up your mouth with your program.

If you are sure that there is an error, but it still does not appear on the screen - find the error log of the web server. Usually, this is a file called error_log. Where it is - you should look in the documentation or ask the support service provider.

If you have problems with the mysql functions (below the line where the error occurred), you must display mysql_error() and the query itself - for visual control and copying to the forum.

When working with images , to see the error message, you must guess to disable the output of the header telling the browser that the picture goes next.
When you download a file, first look at the $ _FILES array.
If there are problems in the interaction between the server and the client - it is mandatory to watch the exchange of HTTP headers
And always look not at what the browser shows, but INITIAL HTML code!

Suppose an error message appears, and you received it. What to do next? It's very simple - read and correct. If you do not have enough knowledge of English, you should either use an interpreter, or take a significant part of this message and request Google. 90% of the probability that someone with such a mistake has already encountered, and you will immediately read the answer.
If you did not find it, then ask the question in the forum, as if copying a small (3-5 lines) piece of code in which an error occurred, specifying exactly the line about which it says in the error message, as well as the most important thing! - the error message itself.
Agree that with this information you on the forum will help much sooner and better?

Debugging and searching for errors in your algorithm.
But it happens that the program does not cause errors, but still does not work, or it does not work as it should.
There is already a fault or algorithm or some external factors.
However, here you can also find the place where the error occurs.
But only on one condition.
That you clearly understand what your program, each function, each line in it does. Because if you represent, then you can predict what values ​​variables should have at each stage of execution.
And then everything is VERY simple!
First, you need to divide the program into logical blocks.
Suppose the script outputs the form, receives it, and writes the data to the database. THREE steps! And in any of them there can be an error, which leads to the fact that data is not written to the database.
It is necessary to check on each of the sections - do all the variables have the value that is expected.
The program works with variables.
How to check?
Display all variables used on the screen! And visually monitor their contents.
Just just write the problematic places var_dump($var) and it turns out that the variable is empty!
And already you can go to the forum not with the question "I have a 100-line code, where is the error?", But "I wrote a function, but for some reason, when I refer to variables in it, they are all empty." Or "variables are not transferred from the form".
There is an abyss between these two ways of asking questions.
The first one can not help you in any way. You, in fact, yourself do not know what your problem is. And at the second you already know the problem, and if you can not cope with its solution, then you can ask a specific question on the forum.

Still very much helps to avoid errors in the program setting error_reporting in E_ALL from the very beginning of the script.
If, when catching critical errors, messages about potential errors can prevent us from seeing the main one, then when developing it, it is desirable for us to see everything - and potential ones as well. For example, if E_ALL, when accessing a non-existent variable, PHP will issue a warning. That is, you do not have to output the variable yourself to find out that you did not assign it any value - PHP will warn you.

An example of debugging.
From the html form, the checkboxes with the names c_1, c_1, c_3 ... c_10
In the script, we try to output
for ( $i = 1 , $i < 11 , $i ++) {
echo
$_POST [ 'с_$i' ];
}
The script does not print anything.
Begin to debug.
First look at the source code of the html page. Does it comply with the standards?
Let's say it is. So, the problem is not in the form.
Next, we check in the script - is there such a variable to which we are addressing - the $ _POST array?
Write

echo '<pre>' ;
var_dump ( $_POST );
We are convinced that the array is there and all the elements are in place. So, the problem is not in transmission.
Hence, we somehow wrongly refer to the array.
We refer to it like this: $ _POST ['c_ $ i']
It is necessary to check - and in what turns 'with_ $ i'?
Do echo 'c_ $ i'; And see ... not at all what they expected to see.
And now we are going either to read the documentation about the lines in PHP (which is preferable), or - to the forum, with the question "why my variable has not been replaced by its value." Which question will sound much better than "my form does not work."
Clear?

It should be understood that here is an example, Unreal. The algorithm of actions is shown.
In reality, with error_reporting (E_ALL); PHP would immediately show that the array index is wrong.

The most important thing is to know what you want to receive.
About half of the questions on the forums are caused by the fact that a person does something ... NOT KNOWING what exactly!
The most ingenious question of all times and peoples: "my base ate all the translations of the lines from the text."
The man just did not give himself the trouble to see how the HTML looks like he wants to get, and decided that the line translations ate the base.
And so in everything.
An unrecognized genius constructs a complex SQL query, and when asked how the request should look - he only claps his eyes. ALWAYS first make a request with your hands and execute in the console or phpmyadmin! And after they received the requested request and debugged it - you are welcome, make it on the PHP.
Working with a remote host via a socket over the HTTP protocol is the same! First, learn how to do all the commands with your hands, look at the server's responses with your eyes, and then simulate this dialog in PHP.

Remember - on PHP you work only with STRINGS! The HTML page that you create by the script is just a set of lines for PHP! It does not matter that in this set - the img, script or frame. Php for you will not make line translations, do not draw javascript. If you do not know javascript - then do not try to create a program on it using PHP.
When you open a connection to a remote host, you send a string to the socket, you get a string from the socket. Php does not understand anything in these lines and for you dialogue will not lead! This YOU must clearly understand what you want to send to the socket, and what to get! Therefore, take the program telnet, connect to the right host and try first to do what you want to make php.
If your script does not work with sockets - run into telnet to see what happens!
SQL query is a STRING. You have to imagine clearly what request will result from your clever php code! The database server does not understand the constructions intval, date, mktime and so on! It's all a php code. The result of this will be the string of the correct SQL query. Before writing a php code, you should be clear about how SQL query should result from the result!
If you do not execute SQL query 0 print it to the screen and see what's up with your script!

Conclusion.
Debugging is the main occupation of the programmer.
Debugging is the only and most powerful way to find an error in the program.
Debugging consists of two main components:
1. To simplify the example as much as possible. If the program that draws the form does not work, gets the data, writes the form data to the database and outputs it again, then break the program into its components and execute one by one.
If your complex cook routine does not work, write the test in two lines first to make sure that you can at least post and read the cookie.
2. Output of debugging information.
Check the value of EVERY variable! Each value returned by the function!
Locke does not work? Display it and copy it to the browser!
Does the file write an empty string? Check the components of this line at each stage of its creation and display it!
Are you sure that the screen is displayed? Practice writing in a file on the test line! Hammered right into the script! Reduce the number of unknowns!
And always look not at what the browser shows, but INITIAL HTML code!

I hope that I could at least explain the principles of this lesson.
Successful debugging.

By phpfaq.ru