PHP&MYSQL review outline 1
1. PHP syntax
◆ Data type
PHP has only three basic data types: integers, floating point numbers (or real numbers, double precision numbers) and strings. Strings can use single quotes and double quotes, but they have different meanings: variables can only be used within double quotes.
◆ Variables
Add "$" in front of the variable. When using a variable, you do not need to specify (or define) the type of the variable in advance. Different types of data can be assigned to the same variable. But if you want to use global variables, you must use global instructions (or add them to the $GLOBALS[] array). To use static variables, use static instructions.
◆ Array
Using an array does not require specifying its type and size, it can be used directly. Elements 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 will be automatically arranged in order.
◇ Associative array
Use the following assignment statement to generate an associative array:
$students[name]= 'Zhang San';
$students[age]= 20;
$students[tel]= '65032905-8097';
When accessing the database, a record can be used as an associative array, with the field name in square brackets.
◆ Operator
Generally retains the operators of C language. Added string concatenation character "." (use "->" when accessing object members). Added "=>" operator, used to assign initial value to array. In addition, logical AND ("&&") and logical OR ("||") can also be used with "and" and "or", and logical exclusive OR "xor" is added.
◆ Basic sentences
It is required to master the if-else statement, switch-case statement, for statement, while statement, do-while statement, continue statement, and break statement. require statement and include statement, used to insert a disk file. The difference is: if used in a conditional statement, include only inserts the file when the condition is met, while require always inserts. The format is:
include("file name");
require("file name");
◆ Definition and use of functions
Use function to define a function without specifying the function type and parameter type.
Function function name (parameter 1, parameter 2,...)
{ Statement 1;
Statement 2;......
}
It is allowed to add "&" before the parameters so that the parameters can transfer data in both directions. It is also allowed to assign default values to parameters.
2. MYSQL syntax
Numeric type
Column type
Amount of storage required
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 types
Column type
Amount of storage required
DATE
3 bytes
DATETIME
8 bytes
TIMESTAMP
4 bytes
TIME
3 bytes
YEAR
1 byte
String type
Column type
Amount of storage required
CHAR(M)
M bytes, 1 <= M <= 255
VARCHAR(M)
L+1 bytes, where L <= M and 1 <= M <= 255
TINYBLOB, TINYTEXT
L+1 bytes, here L< 2 ^ 8
BLOB, TEXT
L+2 bytes, where L< 2 ^ 16
MEDIUMBLOB, MEDIUMTEXT
L+3 bytes, here L< 2 ^ 24
LONGBLOB, LONGTEXT
L+4 bytes, where L< 2 ^ 32
ENUM('value1','value2',...)
1 or 2 bytes, depending on the number of enumeration 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 Database name
2. Display database
SHOW DATABASES
3. Open the database
USE database name
4. Display the tables in the database
SHOW TABLES
5. Display table structure
DESCRIBE table name or SHOW COLUMNS FROM table name
6. Create table
CREATE TABLE Table name (field name Data type (data size) [NOT NULL][PRIMARY KEY[AUTO_INCREMENT]],...)
7. Modify table
A. Add new domain
Format: ALTER TABLE table name ADD COLUMN field name data type (data size) NOT NULL...
B. Modify domain
Format: ALTER TABLE table name CHANGE COLUMN field name field definition
C. Delete domain
Format: ALTER TABLE table name DROP COLUMN domain name
8. Delete table
Format: DROP TABLE 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 record
update table name set field name = new value where condition
13. Delete records
delete from table name where condition
3. Examples
1. IF…ELSE program
if ($gender=="woman")
echo "
else
echo "<>Hello Sir
?>
2. IF…ELSEIF…ELSE program
Result: echo $num1;?> echo $num2;?>
Equals
if ($operation == "Add")
{$x = $num1 + $num2;
print $x;}
elseif ($operation == "Minus")
{$x = $num1 - $num2;
print $x;}
elseif ($operation == "Multiple")
{$x = $num1 * $num2;
print $x;}
elseif ($operation == "except")
{$x=$num1/$num2;
print $x;}
else
print $x;
?>
3. for loop program
$sum=0;
for ($i=1; $i<=100; $i++) //Enter the loop
{
$sum+=$i; //Execute once to add $sum to $i
}
echo $sum; //Display results
?>
4. while program
$sum=0;
while ($i<=100)
{$sum+=$i;
$i++;
};
echo $sum;
?>
5. do … while program
$sum=0; $i=1;
do {
$sum+=$i;
$i++;}
while ($i<=$up);
echo "Start from 1 and add 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*$cal_nu;return array($cal_sqr, $cal_cub);
}
?>
list($sqr, $cub) = cal($nu_input);
echo $nu_input; echo "The square of "is:"; echo $sqr;
echo "
echo $nu_input; echo "The cube of " is: "; echo $cub;
?>
7. Create 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
?>
8. Add record
$cn=mysql_connect("localhost","s990402","zq");
mysql_select_db("s990402",$cn);
$ins=mysql_query("INSERT INTO students(nam,age,tel,addr)
if($ins)
echo "New records have been added to the database.";
else
echo "Record addition failed.";
?>
9. Browsing history
姓名 | 年龄 | 电话 | 住址 |
$a[name] | $a[age] | $a[tel] | $a[addr] |
10. 删除记录(本程序文件名为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);
?>
姓名 | 年龄 | 电话 | 住址 | |
删除 | $a[nam] | $a[age] | $a[tel] | $a[addr] |