Home >Backend Development >PHP Tutorial >Summary of some error handling methods and techniques in PHP_PHP Tutorial
1: Why can’t I get the variable?
I POST data name from one web page to another web page, why can’t I get any value when I output $name?
In PHP4. In versions 2 and later, register_global defaults to off
If you want to get the variables submitted from another page:
Method one: Find register_global in PHP.ini and set it to on.
Method two: Put this extract($_POST);extract($_GET); at the front of the receiving web page (note that there must be Session_Start() before extract($_SESSION)).
Method 3:Read variables $a=$_GET["a"];$b=$_POST["b"], etc. one by one. Although this method is troublesome, it is safer.
2: Debugging your program
You must know the value of a certain variable at runtime. This is what I did, create a file debug.php with the following content:
PHP code:-------------------------- ---------
"; <br>Echo "The _GET variables obtained on this page are:"; <br>Print_R($_GET); <br>Echo "The _POST variables obtained on this page There are: "; <br>Print_R($_POST); <br>Echo "The _COOKIE variables obtained on this page are:"; <br>Print_R($_COOKIE); <br>Echo "The _SESSION variables obtained on this page There are: "; <br>Print_R($_SESSION); <br>Echo "";
3: How to use session
Everything related to session must call the function session_start() before;
Paying value for session is very simple, such as:
Tip 2:
If your Session_Start() is placed in a loop statement and it is difficult to determine where the information was output to the browser before, you can use the following method:
1 Line [php] Ob_Start(); [/php]
.....Here is your program...
2: What is the error?
Warning: session_start( ): open(/tmpsess_7d190aa36b4c5ec13a5c1649cc2da23f, O_RDWR) failed:....
Because you did not specify the storage path of the session file.
Solution:
(1) Create the folder tmp in the c drive
(2) Open php.ini, find session.save_path, and change it to session.save_path= "c: /tmp"
4: Why when I send variables to another web page, I only get the first half, and all the ones starting with spaces are lost
"; <br>Echo $_GET["Name"]; <br>Echo "< ;/pre>"; <br> </div> <br>The correct method is: <br><div class="codetitle"> <span style="CURSOR: pointer" onclick="doCopy('code44918')"><u>Copy the code</u></span> The code is as follows:</div> <div class="codebody" id="code44918"> <br>$Var="hello php"; <br>$post= "receive.php?Name=".urlencode($Var); <br>header("location:$post"); <br> </div> <br>You don’t need to use Urldecode() on the receiving page, the variables will be automatically encoded. <p><strong>5: How to intercept Chinese characters of a specified length without ending with "[/php]", and the excess part is replaced with "..."<br></strong>Generally speaking, the variables to be intercepted come from Mysql, first ensure that the field length is long enough, usually char(200), which can hold 100 Chinese characters, including punctuation. <br></p> <div class="codetitle"> <span style="CURSOR: pointer" onclick="doCopy('code64808')"><u>Copy code</u></span> The code is as follows :</div> <div class="codebody" id="code64808"> <br>$str="This character is so long, ^_^"; <br>$Short_Str=showShort($str,4);//Intercept the first 4 Chinese characters, the result is: This character... <br>Echo "$Short_Str"; <br>Function csubstr($str,$start,$len) <br>{ <br>$strlen=strlen($str); <br>$clen =0; <br>for($i=0;$i<$strlen;$i++,$clen++) <BR>{ <BR>if ($clen>=$start+$len) <br>break; <br>if(ord(substr($str,$i,1))>0xa0) <br>{ <br>if ($clen>=$start) <br>$tmpstr.=substr($str,$i ,2); <br>$i++; <br>} <br>else <br>{ <br>if ($clen>=$start) <br>$tmpstr.=substr($str,$i,1 ); <br>} <br>} <br>return $tmpstr; <br>} <br>Function showShort($str,$len) <br>{ <br>$tempstr = csubstr($str,0, $len); <br>if ($str<>$tempstr) <br>$tempstr .= "..."; //What you want to end with, just modify it here. <br>return $tempstr; <br>} <br> </div> <br><strong>6: Standardize your SQL statements <br></strong>Add "`" in front of tables and fields so that they will not appear due to misuse of keywords Wrong, <br>Of course I don’t recommend you to use keywords. <p><strong>For example<br></strong>$Sql="INSERT INTO `xltxlm` (`author`, `title`, `id`, `content`, `date`) VALUES ('xltxlm', ' use`', 1, 'criterion your sql string ', '2003-07-11 00:00:00')" <br>How to enter "`"? On the TAB key. </p> <p><strong>7: How to make the Html/PHP format string not to be interpreted, but displayed as it is <br></strong></p> <div class="codetitle"> <span style="CURSOR: pointer" onclick="doCopy('code31696')"><u>Copy code</u></span> Code As follows:</div> <div class="codebody" id="code31696"> <br>$str="<h1>PHP</h1>"; <br>Echo "Interpreted: ".$str."<br>Processed: "; <br>Echo htmlentities(nl2br($str)); <br> </div> <br><strong>8: How to get the variable value outside the function in the function<br></strong><div class="codetitle"> <span style="CURSOR: pointer" onclick="doCopy('code31032')"><u>Copy code</u></span> The code is as follows:</div> <div class="codebody" id="code31032"> <br>$a="PHP"; <br>foo(); <br>Function foo() <br>{ <br> global $a;//Delete here to see what the result is <br> Echo "$a"; <br>} <br> </div> <br><strong>9: How do I know what functions are supported by the system by default? <br></strong><div class="codetitle"> <span style="CURSOR: pointer" onclick="doCopy('code6465')"><u>Copy code</u></span> The code is as follows:</div> <div class="codebody" id="code6465"> <br>$arr = get_defined_functions(); <br>Function php() { <br>} <br>echo "<pre class="brush:php;toolbar:false">"; <br>Echo "This displays all functions supported by the system, and the custom function phpn"; <br>print_r($arr); <br>echo "";
12: I want to add a file at the beginning and end of each file. But adding them one by one is troublesome
1: Open the php.ini file
Set include_path= "c:"
2: Write two files
auto_prepend_file.php and auto_append_file.php are saved in the c drive, they will be automatically attached to the head and tail of each php file.
3: Found in php.ini:
Automatically add files before or after any PHP document.
auto_prepend_file = auto_prepend_file.php; attached to the head
auto_append_file = auto_append_file .php; attached to the tail
In the future, each of your php files will be equivalent to
4: Run the following program to test
16: I want to modify the MySQL user and password
First of all, I must declare that in most cases, modifying MySQL requires root permissions in mysql.
So ordinary users Password cannot be changed without requesting an administrator.
Method 1
Use phpmyadmin, this is the simplest, modify the user table of the mysql library,
But don’t forget to use the PASSWORD function.
Method 2
Using mysqladmin, this is a special case stated earlier.
Mysqladmin -u root -p password mypasswd
After entering this command, you need to enter the original password of root, and then the root password will be changed to mypasswd.
Change root in the command to your username, and you can change your own password.
Of course, if your mysqladmin cannot connect to the mysql server, or you cannot execute mysqladmin,
then this method is invalid.
And mysqladmin cannot clear the password.
The following methods are used at the mysql prompt and must have root permissions for mysql:
Method 3
mysql> INSERT INTO mysql.user ( Host,User,Password)
VALUES('%','jeffrey',PASSWORD('biscuit'));
mysql> FLUSH PRIVILEGES
To be precise, this is adding a user with the user name jeffrey, the password is biscuit.
There is this example in the "mysql Chinese Reference Manual", so I wrote it out.
Be careful to use the PASSWORD function, and then use FLUSH PRIVILEGES.
Method 4
Same as method 3, except that the REPLACE statement is used
mysql> REPLACE INTO mysql.user (Host,User,Password)
VALUES('%' ,'jeffrey',PASSWORD('biscuit'));
mysql> FLUSH PRIVILEGES
Method 5
Use the SET PASSWORD statement,
mysql> SET PASSWORD FOR jeffrey@"%" = PASSWORD('biscuit');
You must also use PASSWORD() Function,
But there is no need to use FLUSH PRIVILEGES.
Method 6
Use GRANT... IDENTIFIED BY statement
mysql> GRANT USAGE ON *.* TO jeffrey@"%" IDENTIFIED BY 'biscuit';
Here PASSWORD () function is unnecessary, and there is no need to use FLUSH PRIVILEGES.
Note: PASSWORD() [does not] perform password encryption in the same way as Unix password encryption.
17: I want to know which website he connected to this page through
19: How to read the current address bar information
24: How to remotely connect to Mysql database
There is a host field in the mysql table of adding users, change it to "%", or specify the IP address that allows the connection, so that you can Called remotely.
$link=mysql_connect("192.168.1.80:3306","root","");
25: How to use regular expressions
Special characters in regular expressions
26: After using Apache, garbled characters appear on the homepage
Method 1:
AddDefaultCharset ISO-8859-1 is changed to AddDefaultCharset off
method Two:
AddDefaultCharset GB2312
====================================== ===================
tip:
When you post the code, GB2312 will be interpreted as??????
Changed to This way it won’t
GB2312
10: How to compare the number of days between two dates, (simpler algorithm)
28: How to keep the program running instead of stopping after more than 30 seconds
set_time_limit(60)//The maximum running time is one minute
set_time_limit(0)//Run until the program ends by itself, or Manual stop
29: Calculate the number of people currently online
Example 1: Use text to implement
30: What is a template and how to use it
I use phplib template
Here are the uses of several functions
$T->Set_File("Define whatever you want ","Template file.tpl");
$T->Set_Block("defined in set_file","","define as you like");
$T->Parse("Defined in Set_Block","",true);
$T->Parse("Output the result as you like"," ");
defined in Set_File sets the loop format to:
How to generate the template Static web page