Fortune Telling Collection - Fortune-telling birth date - Basic course of PHP introduction
Basic course of PHP introduction
PHP was founded by Rasmus Ledorf in 1994. At first, it was a simple program written by Rasmus Ledorf in Perl language to maintain personal web pages. These tools are used to show Rasmus Lerdorf's resume and count network traffic. Later, it was rewritten in C language, including access to the database. He integrated these programs with some form translators and called them PHP/FI. PHP/FI can connect with database to generate simple dynamic web programs. To help you start learning php, I will share the PHP introductory tutorial as follows:
1, embedding method:
2. Reference documents:
There are two ways to reference files: request and include.
The usage of require is as follows: require ("myrequirefile.php"); . This function is usually placed in front of a PHP program. Before the PHP program is executed, it will read the file specified by require and make it a part of the PHP program's web page. Common functions can also be introduced into web pages in this way.
Including using methods such as include ("myincludeFile. PHP"); . This function is usually placed in the processing part of process control. The PHP program web page will only read the include file when reading it. In this way, the process of program execution can be simplified.
3. Marking method:
& lt? Server-side programming language (abbreviation of professional hypertext preprocessor)
Echo "This is the first example. \ n "; //This example is a comment on C++ syntax (PHP comments are similar to C! )
/* This example uses multiple lines.
Annotation method */
Echo "This is the second example. \ n ";
This is Echo third example. \ n "; # This example is annotated with UNIX Shell syntax.
? & gt
4. Variable type:
$mystring = "I am a string";
$ NewLine = " newline \ n
$ int 1 = 38;
$ float 1 = 1.732;
$ float 2 = 1.4E+2;
$MyArray 1 = array ("Zi", "Ugly", "Yin", "Mao");
There are two problems. First, the PHP variable starts with $,and the second PHP statement starts with $. Finally, ASP programmers may not adapt. These two omissions are also the most mistakes in the program.
5, operation symbol:
Quantity * * Illegal words have been blocked * * Count:
symbolic meaning
+ ? Add operation
-subtraction operation
* Multiplication operation
/division operation
% ? Take the remainder
++accumulation
- ? Gradually/gradually/gradually/continuously decrease.
String operation:
There is only one operation symbol, which is the period in English. It can concatenate strings and become a new merged string. Similar to&&
& lt?
$ a = " PHP 4
$b = "powerful";
echo $ a . $ b;
? & gt
There are also two problems. First, the output statement in PHP is echo, and second, it is similar to.
Logical operation:
symbolic meaning
Less than & lt
& gt greater than ...
& lt= less than or equal to
& gt= greater than or equal to
= = equal to
! = not equal to
& and (and)
And (and)
Or (or)
Or or (or)
XOR (exclusive OR)
! No (not)
Learning purpose: to master the process control of php.
1, if ... else loop has three structures.
The first is that only when conditions are used as simple judgments. What to do if something happens. The syntax is as follows:
if (expr) { statement }
Expr is the condition of judgment, usually based on logical operation symbols. A statement is a qualified execution part of a program. If the program has only one line, you can omit the braces.
Example: In this example, the braces are omitted.
& lt? Server-side programming language (abbreviation of professional hypertext preprocessor)
If ($state== 1) echoes "haha";
? & gt
It is particularly important to note that it is = = instead of = that judges equality, which may be a common mistake made by ASP programmers. = is an assignment.
Example: There are three lines in the operation part of this example, and braces cannot be omitted.
& lt? Server-side programming language (abbreviation of professional hypertext preprocessor)
if ($state== 1) {
Echo "haha;
Echo "& ltbr >;; ;
}
? & gt
The second is to add the condition of else to if, which can be interpreted as "what to do if something happens, or how to solve it otherwise". The grammar is as follows
If (expr) {statement1} else {statement2} example: modify the above example to make it more complete. Only one line of instructions is executed in else, so there is no need to increase parentheses.
& lt? Server-side programming language (abbreviation of professional hypertext preprocessor)
if ($state== 1) {
Echoing "haha";
Echo "& ltbr >;; ;
}
Otherwise {
Echo "hehe";
Echo "& ltbr >;; ;
}
? & gt
The third is the recursive if..else loop, which is usually used for various decision-making judgments. It combines several if-or-otherwise treatments.
Look directly at the following example.
& lt? Server-side programming language (abbreviation of professional hypertext preprocessor)
if($ a & gt; $b ) {
Echoing "A is greater than B";
} else if $ a = = $ b){
Echoing "A equals B";
} Otherwise {
Echo "A is smaller than B";
}
? & gt
The above example only uses two layers of if...else loop to compare two variables A and B. Please be careful when using this recursive if..else loop in practice, because too many layers of the loop will easily lead to problems in design logic, or too few curly braces will lead to inexplicable problems in the program.
2. There is only one 2.for loop, and there is no change. Its syntax is as follows.
for(expr 1; Expr2 expression 3) {statement}
Where expr 1 is the initial value of the condition. Expr2 is the condition of judgment, which is usually judged by logical operators. Expr3 is the part to be executed after the statement is executed, which is used to change the conditions of the next loop judgment, such as adding 1 ... and so on. A statement is a qualified execution part of a program. If the program has only one line, you can omit the braces.
The following example is written in the for loop.
& lt? Server-side programming language (abbreviation of professional hypertext preprocessor)
for($ I = 1; $ i & lt= 10 ; $i ++) {
Go along with "here it is". I. "Cycle
}
? & gt
3. Switch loop, which usually deals with compound condition judgment. Each sub-condition is a case instruction part. If many similar IF instructions are used in practice, they can be integrated into a switching loop.
The syntax is as follows
switch(expr){ case expr 1:statement 1; Break; Case expr2: statement 2; Break; Default value: statementN breaks; }
Where the expr condition is usually a variable name. ExprN after case usually represents the value of a variable. The colon is followed by the part to be executed that meets the conditions. Be careful to break out of the loop.
& lt? Server-side programming language (abbreviation of professional hypertext preprocessor)
Switch (date ("d") (
Case "Mon":
Echo "Today is Monday";
Break;
Case "Tuesday":
Echo "Today is Tuesday";
Break;
Case "Marriage":
Echo "Today is Wednesday";
Break;
Case "Thu":
Echo "Today is Thursday";
Break;
Case "Fri":
Echo "Today is Friday";
Break;
Default value:
Echo "Today is a holiday";
Break;
}
? & gt
What needs to be noted here is that break should not be missed. By default, it is ok to miss it.
Obviously, the above example is very troublesome to use if loop. Of course, when designing, we should put the conditions with the highest probability in front and the conditions with the lowest probability in the back, which can increase the execution efficiency of the program. Because the probability of the above example is the same every day, you don't have to pay attention to the order of conditions.
Learn to build a database
In PHP, the command line editing of my SQL may be troublesome for beginners. It doesn't matter, you can download and install a PHPMYADMIN, and you can rely on it to build an editing database in the future.
Let's talk about its purpose first.
After entering phpmyadmin, we first need to build a database.
Language (*) Select Simplified Chinese here, and then create a new database on the left. Fill in the database name here, and then click Create.
Then select the created database from the drop-down menu on the left. potential
To create a new table in the database store:
Name:
Number of fields:
Fill in the name of the form and the approximate number of fields you think (it doesn't matter if there are not enough, you can add or default later) and press Execute.
Then you can start building the table.
The first column is the name of the field; Select the field type in the second column:
We often use the following:
1)VARCHAR, text type
2)INT, integer type
3)FLOAT, floating-point type
4) Date, date type
5) You may ask, where is the automatically added ID? Just select the INT type and select auto_increment in the extra below.
After creating the table, you can see the table you created on the left. After clicking, you can:
1) Press the structure on the right: View and modify the table structure.
2) Click Browse on the right: View the data in the table.
3) Press SQL on the right to run the SQL statement.
4) Press Insert on the right: insert a row of records.
5) Press Clear on the right: delete all records in the table.
6) Press Delete on the right: Delete the table.
Another very important function is import and export. When we finish the program and database locally, we need to have a local image on the server. If ASP access is simple, we can upload MDB files directly. If it is SQL SERVER, we can also connect to a remote server for import. Then you can export all the SQL statements in MY SQL, go to PHPMYADMIN of the remote server, press SQL after creating the database, and paste all the SQL statements generated at this level just copied.
Learn to connect to the database.
PHP is just a function library, and the rich functions make some parts of PHP quite simple. I suggest downloading a PHP function manual, which has been used.
Here is a brief talk about connecting MYSQL database.
1、mysql_connect
Open MySQL server connection.
Syntax: int MySQL _ connect(string[ host name] [:port], string[ user name], string[ password]); Return value: integer
This function establishes a connection with the MySQL server. All parameters can be omitted. When this function is used without any parameters, the default value of parameter hostname is localhost, the default value of parameter username is the owner of PHP execution trip, and the parameter password is an empty string (that is, there is no password). The parameter hostname can be followed by a colon and a port number to indicate which port to use to connect to MySQL. Of course, when using the database, using mysql_close () to close the connection early can save resources.
2、mysql_select_db
Select a database.
Syntax: int MySQL _ select _ db (string database _ name, int[ link _ identifier]); Return value: integer
This function selects the database in MySQL server for future data query. Returns true on success and false on failure.
The simplest example is:
$ conn = MySQL _ connect(" 127 . 0 . 0 . 1 ",""," ");
MySQL _ select _ db(" shop ");
Connect to my SQL database and open the store database. In practical application, we should strengthen some wrong judgments.
Learn to read data
Look at two functions first:
1、mysql_query
Send a query string. Syntax: int MySQL _ query (string query, int [link _ identifier]); Return value: integer
This function sends the query string to MySQL for related processing or execution. If the link_identifier parameter is not specified, the program will automatically find the recently opened ID. When the query string is updated, inserted and deleted, it may be true or false; ; If the query string is SELECT, a new ID value will be returned. When false is returned, the execution is not successful but no return value is returned, but the query string is wrong.
2.mysql_fetch_object returns class data. Syntax: object MySQL _ fetch _ object (int result, int [result _ typ]); Return value: class
This function is used to split the query results into class variables. If the result has no data, it will return a false value.
Look at a simple example:
& lt?
$ exec = " select * from user
$ result = MySQL _ query($ exec);
while($ RS = MySQL _ fetch _ object($ result))
{
Echo "User Name:". $ RS-& gt; User name. “& ltbr >; ;
}
? & gt
Of course, there is a user name field in the table User, similar to that in asp.
& lt%
Exec= "Select from Users *
set RS = server . createobject(" adodb . recordset ")
Connecticut, 1, 1
Do without doing.
Response.write "User name: & amprs ("User name ") & amp“& ltbr >;;
rs.movenext
ring
% & gt
Of course, we must connect to the database first. Generally, we need _ once ('conn.php'); In conn.php, there is code to connect to the database mentioned last time.
Two small commands can complete the reading of data.
Learn to add, delete and modify data.
MySQL _ query($ exec);
This statement itself can perform all operations, but the difference is the sql statement $exec.
Add: $ exec = "insert into tablename (item1,item 2) value ('). $ _ post ['item 1']。 ," $ _ post ['item 1']") ";
delete:$ exec = " delete from tablename where ... ";
Modified: $ exec = "Update TableName SetItem1='". $ _ post ['item 1']。 Where is it ... ";
Having said that, I want to talk about forms and php variable passing. If one of these forms
If the form is submitted by POST, you can use $_POST['item 1'] to get the variable value when processing the form file. Similarly, the form submitted by get is $_GET['item 1'].
Is it simple? But usually $exec will have a problem, because your SQL statement may be very long and you will miss it. Connectors or' to enclose character fields.
We can annotate MySQL _ query ($ exec); This statement uses echo $exec instead of outputting $ exec to check the correctness. If you still can't find out the error of $exec, you can copy this sql statement and execute it in phpmyadmin to see its error information. It is also important to note that we should not use some sensitive strings as field names, otherwise there may be problems, such as date. The naming of variables and fields follows a small rule, which is sometimes beneficial to oneself, and novices cannot ignore its importance.
Learn to use conversation.
SESSION has many functions, the most commonly used is variable transfer between pages within the site.
At the beginning of the page, we want session _ start (); Public meeting; ;
You can then use session variables. For example, the assignment is: $ _ session ['item'] = "item1"; The value to be obtained is $ item1= $ _ session ['item']; It's simple. We may use some functions here, such as judging whether a session variable is empty, so it can be written as: empty($_SESSION['inum']) returns true or false.
Let's look at a login procedure to determine whether the user name and password are correct.
The login form is like this: login.
& lttable width = " 100% " height = " 100% " border = " 0 " align = " center " cell padding = " 0 " cellspacing = " 0 " >
& lttr & gt
& ltform action = " checklog in . PHP " method = " post " & gt; & lttd align="center" valign="middle " >& lttable width = " 400 " border = " 0 " cell padding = " 5 " cellspacing = " 1 " class = " table BG " >。
& lttr class="tdbg " >
& lttd colspan = "2" > & ltpalign = "center" > administrator login & lt/p & gt;; & lt/TD & gt;
& lt/tr & gt;
& lttr class="tdbg " >
& lttd & gt& ltp align="center "> user name & lt/p > & lt/TD & gt;
& lttd & gt& ltp align="center " >
& ltinput name = " username " type = " text " id = " username " >
& lt/p & gt; & lt/TD & gt;
& lt/tr & gt;
& lttr class="tdbg " >
& lttd & gt& ltp align="center "> password & lt/p > & lt/TD & gt;
& lttd & gt& ltp align="center " >
& ltinput name = " password " type = " password " id = " password " & gt;
& lt/p & gt; & lt/TD & gt;
& lt/tr & gt;
& lttr class="tdbg " >
& lttd colspan="2 " >& ltp align="center " >
& ltinput type = " Submit " name = " Submit " value = " Submit " & gt;
& ltinput type = " reset " name = " submit 2 " value = " Clear " & gt。
& lt/p & gt; & lt/TD & gt;
& lt/tr & gt;
& lt/table & gt; & lt/TD & gt; & lt/form & gt;
& lt/tr & gt;
& lt/table & gt;
Here's how to handle files.
& lt? Server-side programming language (abbreviation of professional hypertext preprocessor)
require _ once(' conn . PHP ');
session_start()。
$ username = $ _ POST[' username '];
$ password = $ _ POST[' password '];
$ exec = " select * from admin where username = ' "。 $username。 '";
if($result=mysql_query($exec))
{
if($ RS = MySQL _ fetch _ object($ result))
{
If ($ RS-& gt;; Password = = $ password)
{
$ _ SESSION[' admin name ']= $ username;
header(" location:index . PHP ");
}
other
{
Echo "< script & gtalert ('Password check error!' ); location . href = ' log in . PHP '; & lt/script & gt; ";
}
}
other
{
Echo "< script & gtalert ('User name check error!' ); location . href = ' log in . PHP '; & lt/script & gt; ";
}
}
other
{
Echo "< Script & gt Alert ("Database connection error! " ); location . href = ' log in . PHP '; & lt/script & gt; ";
}
? & gt
Conn.php is like this:
& lt? Server-side programming language (abbreviation of professional hypertext preprocessor)
$ conn = MySQL _ connect(" 127 . 0 . 0 . 1 ",""," ");
MySQL _ select _ db(" shop ");
? & gt
Because $ _ session ['adminname'] = $ username; We can write a file like this to verify login: checkadmin.php.
& lt? Server-side programming language (abbreviation of professional hypertext preprocessor)
session_start()。
if($_SESSION['adminname']== ' ')
{
Echo "< script & gtalert ('please log in first'); location . href = ' log in . PHP '; & lt/script >;
}
? & gt
Paging display
The key is to use limit in SQL statements to limit the number of records displayed. We need a variable $page to record the current page, and we also need the total number of records of $num.
For $page, if there is no page, let it =0, if there is.
$ execc = " select count(*)from tablename ";
$ resultc = MySQL _ query($ execc);
$ RSC = MySQL _ fetch _ array($ resultc);
$ num = $ RSC[0];
This can get the total number of records.
Ceil($num/ 10)) If a page records 10, this is the total number of pages.
So you can write it like this
if(empty($_GET['page']))
{
$ page = 0;
}
other
{
$ page = $ _ GET[' page '];
if($ page & lt; 0)$ page = 0;
if($ page & gt; = ceil($ num/ 10))$ page = ceil($ num/ 10)- 1; //Because page starts from 0, it should be-1.
}
In this way, $exec can write $ exec = "select * from tablename limit". ($ page * 10)。 ", 10";
// 10 records one page.
The last thing we need to do is some connections:
& lta href="xxx.php? Page=0 "> home page & lt/a & gt;;
& lta href="xxx.php? page = & lt? =($page- 1)? >& gtPrevPage & lt/a & gt;
& lta href="xxx.php? page = & lt? =($page+ 1)? >& gtNextPage & lt/a & gt;
& lta href="xxx.php? page = & lt? =ceil($num/ 10)- 1? >& gtLastPage & lt/a & gt;
Matters needing attention
1, be careful not to leave out the semicolon.
2. Be careful not to miss the $ before the variable.
3. When using SESSION, be careful not to leave out session _ start ();
If an error occurs, you can adopt the following methods:
1. If there is an error in the SQL statement, please comment it and then output the SQL statement. Pay attention to comments and adjust subsequent SQL statements.
2. If a variable is empty, it is usually not passed in place. Check the output variables, and check the id and name of the form.
3. If there is an error in the database connection, check whether my SQL is opened correctly and whether the connection statement is missing.
4. Pay attention to indentation and eliminate the error of bracket mismatch.
When building a big website, my idea is to build a database first to determine the function of each field and the relationship between tables. Then design the background interface, starting with adding data, because the successful addition can be directly verified in the database, and the page displayed after the addition is completed, and finally the combination of the two. Generally speaking, the background includes adding, deleting, modifying and displaying. There is no problem backstage, and there is no big problem at the front desk. The front desk also needs to pay attention to security and fault tolerance, as well as the output format.
Learn to upload files and send emails in PHP.
Enctype="multipart/form-data "must be added to the file upload form.
and
Let's look at the code:
$ f = & amp$ HTTP _ POST _ FILES[' file '];
$ dest _ dir =' uploads// Set the upload directory.
$dest=$dest_dir。 /'.Date ("ymd"). "_".$ f[' name ']; //I set the file name as the date and add the file name here to avoid duplication.
$ r = move _ uploaded _ file($ f[' tmp _ name '],$ dest);
chmod($dest,0755); //Set the properties of the uploaded file.
The uploaded file name is Date ("YMD"). "_".$f['name'], which can be used when inserting the database in the future. PHP actually moves the uploaded files from the temporary directory to the specified directory. move _ uploaded _ file($ f[' tmp _ name '],$ dest); This is the key.
As for sending emails, it's even simpler. You can use the mail () function.
Mail ("recipient address", "subject", "body", "sender: sender \ r \ reply: sender address");
However, mail () needs the support of the server, and the SMTP server needs to be configured under WINDOWS. Generally speaking, the LINUX space outside is enough.
It seems that uploading files and sending emails are much simpler than ASP. Just call the function. ASP also needs to use different components of the server, such as FSO and JMAIL.
;
- Previous article:Datang fortune-telling _ Datang fortune-telling
- Next article:How did my years as a teacher of Yin and Yang end?
- Related articles
- What do you mean, soft-hearted and cruel?
- Fortune-telling marriage formula
- Fortune-telling birthday 1988 On the 25th day of September in the lunar calendar, in the morning, just before dawn, what is your life? Does it taste good?
- What does calf fortune-telling mean?
- The Secret of Ji Xiaolan's Fortune Telling _ What is the secret of Ji Xiaolan's fortune telling?
- Gregorian calendar or Gregorian calendar _ Gregorian calendar or Gregorian calendar
- The online fortune teller said that I would die this year. Can you believe it?
- Something unpleasant happened at work recently, and I feel unhappy, too. However, I painted the Guanyin watch for several days. How should this be explained?
- 1fortune telling for people born on July 7, 967.
- There are scar faces among women.