PHP & MYSQL overview

Source: Internet
Author: User
PHP & amp; MYSQL review outline 1. PHP syntax ◆ data types PHP only contains three basic data types: integer, floating point (or real number, double precision), and string. Strings can be enclosed in single quotes and double quotes, but they have different meanings: variables can only be used in double quotes. ◆ Variable... SyntaxHighlighter. all ();

PHP & MYSQL Overview 1

I. PHP syntax

◆ Data type

PHP only has three basic data types: integer, floating-point number (or real number, double-precision number), and string. Strings can be enclosed in single quotes and double quotes, but they have different meanings: variables can only be used in double quotes.

◆ Variable

You must add "$" before the variable. you do not need to specify (or define) the type of the variable before using the variable. you can assign values to the same variable for different types of data. However, to use a global variable, use the global description (or add it to the $ GLOBALS [] array ). Static variables must be used.

◆ Array

You can use an array directly without specifying its type and size. Each element of the same array can have different data types.

◇ Scalar array

Use the following assignment statement to generate a scalar array:

$ A [0] = 100;
$ A [1] = "Hello ";
$ A [2] = 23.4;

If the subscript is omitted, the subscript values are automatically arranged in order.

◇ Join array

Use the following assignment statement to generate an associated array:

$ Students [name] = 'Zhang San ';
$ Students [age] = 20;
$ Students [tel] = '2014-65032905 ';

When accessing the database, a record can be used as an associated array with the field name in square brackets.

◆ Operator

Generally, operators in the C language are retained. Added the string Connector "." (used to access object members "-> "). Added the "=>" operator to assign initial values to the array. In addition, logic and ("&") and logic or ("|") are also available "and" and "or", adding logical differences or "xor ".

◆ Basic statements

Master the if-else statements, switch-case statements, for statements, while statements, do-while statements, continue statements, and break statements. The require and include statements are used to insert a disk file. The difference is: if it is used in conditional statements, include inserts files only when the conditions are met, while require always inserts files. Format:

Include ("file name ");
Require ("file name ");

◆ Function definition and use

You do not need to specify the function type and parameter type when using the function to define a function.

Function name (parameter 1, parameter 2 ,......)
{Statement 1;
Statement 2 ;......
}

You can add "&" before the parameter so that the parameter can transmit data in two directions. You can also assign default values to parameters.

II. MYSQL syntax

Value type

Column type

Required storage capacity

TINYINT

1 byte

SMALLINT

2 bytes

MEDIUMINT

3 bytes

INT

4 bytes

INTEGER

4 bytes

BIGINT

8 bytes

FLOAT (X)

4 If X <= 24 or 8 if 25 <= X <= 53

FLOAT

4 bytes

DOUBLE

8 bytes

DOUBLE PRECISION

8 bytes

REAL

8 bytes

DECIMAL (M, D)

M bytes (D + 2, if M <D)

NUMERIC (M, D)

M bytes (D + 2, if M <D)

Date and time type
Column type

Required storage capacity

DATE

3 bytes

DATETIME

8 bytes

TIMESTAMP

4 bytes

TIME

3 bytes

YEAR

1 byte

String type
Column type

Required storage capacity

CHAR (M)

M bytes, 1 <= M <= 255

VARCHAR (M)

L + 1 byte, where L <= M and 1 <= M <= 255

TINYBLOB, TINYTEXT

L + 1 byte, where L <2 ^ 8

BLOB, TEXT

L + 2 bytes, here L <2 ^ 16

MEDIUMBLOB, MEDIUMTEXT

L + 3 bytes, here L <2 ^ 24

LONGBLOB, LONGTEXT

L + 4 bytes, here L <2 ^ 32

ENUM ('value1', 'value2 ',...)

1 or 2 bytes, depending on the number of enumerated values (maximum 65535)

SET ('value1', 'value2 ',...)

1, 2, 3, 4, or 8 bytes, depending on the number of set members (up to 64 members)

1. create a new database

Create database name

2. display the database

SHOW DATABASES

3. open the database

USE database name

4. display tables in the database

SHOW TABLES

5. display table structure

DESCRIBE table name or show columns from table name

6. create a table

Create table name (domain name data type (data size) [not null] [primary key [AUTO_INCREMENT], ......)

7. modify a table

A. New Domain

Format: alter table name add column domain name data type (data size) not null...

B. modify the domain

Format: alter table name change column domain name Domain definition

C. delete a domain

Format: alter table name drop column domain name

8. delete a table

Format: drop table name

9. select query

Format: SELECT domain name [AS domain alias]... FROM table name [WHERE condition] [group by...] [HAVING...] [order by...]

10. add a single record

Insert into table name (Field 1, field 2,...) values (value 1, value 2 ,...)

11. add multiple records

Insert into table name (Field 1, field 2,...) select field from table where condition;

12. update records

Update table name set domain name = new value where condition

13. delete records

Delete from table name where condition


III. instances

1. IF... ELSE Program

If_else.php

 

If ($ gender = "woman ")

Echo "Miss Hao ";

Else

Echo "<> good sir ";

?>

2. IF... ELSEIF... ELSE Program

Simple Calculator


Result:

Equal

If ($ operation = "plus ")

{$ X = $ num1 + $ num2;

Print $ x ;}

Elseif ($ operation = "minus ")

{$ X = $ num1-$ num2;

Print $ x ;}

Elseif ($ operation = "multiplication ")

{$ X = $ num1 * $ num2;

Print $ x ;}

Elseif ($ operation = "")

{$ X = $ num1/$ num2;

Print $ x ;}

Else

Print $ x;

?>


3. for loop program

Calculate 1 + 2 +... + 100 value

 

$ Sum = 0;

For ($ I = 1; $ I <= 100; $ I ++) // enters the loop

{

$ Sum + = $ I; // add $ sum to $ I after one execution.

}

Echo $ sum; // display the result

?>

4. while Program

While. php

 

$ Sum = 0;

While ($ I <= 100)

{

$ Sum + = $ I;

$ I ++;

};

Echo $ sum;

?>

5. do... While Program

Do_while.php

 

$ Sum = 0; $ I = 1;

Do {

$ Sum + = $ I;

$ I ++;

}

While ($ I <= $ up );

Echo "add from 1 to". ($ i-1 );

Echo"
";

Echo "the sum is". $ sum;

?>

6. function routine

Function cal ($ cal_nu)

{

$ Cal_sqr = $ cal_nu * $ cal_nu;

$ Cal_cub = $ cal_nu * $ cal_nu;

Return array ($ cal_sqr, $ cal_cub );

}

?>

Calculate the sum of squares

 

List ($ sqr, $ cub) = cal ($ nu_input );

Echo $ nu_input; the square of echo "is:"; echo $ sqr;

Echo"
";

Echo $ nu_input; echo ":"; echo $ cub;

?>

7. create a data table

Mysql_connect ("localhost", "s990402", "zq ");
Mysql_select_db ("s990402 ");
$ Str = "create table students (
Id int not null AUTO_INCREMENT primary key,
Name CHAR (10 ),
Age INT,
Tel VARCHAR (20 ),
Addr VARCHAR (30)

)";
$ Result = mysql_query ($ str );

If ($ result)
Echo "data table \" students \ "created successfully! ";
Else
Echo "an error occurred while creating the data table! ";
?>

8. add record

$ Cn = mysql_connect ("localhost", "s990402", "zq ");
Mysql_select_db ("s990402", $ cn );
$ Ins = mysql_query ("insert into students (nam, age, tel, addr)
VALUES ('$ nam', $ age, '$ tel', '$ addr') ", $ cn );
If ($ ins)
Echo "the new record has been added to the database. ";
Else
An error occurred while adding the echo record. ";
?>

9. browsing history












Mysql_connect ("localhost", "s990402", "zq ");Mysql_select_db ("s990402 ");$ Q = mysql_query ("SELECT * FROM students order by age DESC ");While ($ a = mysql_fetch_array ($ q ))Print"      "?>
Name Age Phone Number Address
$ A [name] $ A [age]$ A [tel] $ A [addr]

10. delete the record (the program name is del. php)

$ Cn = mysql_connect ("localhost", "s990402", "zq ");
Mysql_select_db ("s990402", $ cn );
If ($ id> 0) mysql_query ("delete from students WHERE id = $ id", $ cn );
?>











$ Q = mysql_query ("SELECT * FROM students order by age DESC", $ cn );While ($ a = mysql_fetch_array ($ q ))Print"         "?>
Name Age Phone Number Address
Delete$ A [nam] $ A [age]$ A [tel] $ A [addr]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.