database links and selection and coding
$link =mysql_connect ("localhost", "root", "123456") or Die ("Database connection Failed". Mysql_error ()); Connecting to a database
$sel =mysql_select_db ("login", $link) or Die ("Database selection failed". Mysql_error ()); Select Database
mysql_query ("Set names ' UTF8 '"); Set Database encoding
Add Data
$link =mysql_connect ("localhost", "root", "123456") or Die ("Database connection Failed". Mysql_error ());
$sel =mysql_select_db ("login", $link) or Die ("Database selection failed". Mysql_error ());
mysql_query ("Set names ' UTF8 '", $sel);
$add = "INSERT INTO title" VALUES (' $title ', ' $content ', ' $username ', $time) ";
$query =mysql_query ($add);
if ($query) {
echo "Add sucess";
echo "<meta http-equiv=\" refresh\ "content=\" 1; Url=show_message.php\ "/>";
}
else echo "add false";
Delete Data
$link =mysql_connect ("localhost", "root", "123456") or Die ("Database connection Failed". Mysql_error ());
$sel =mysql_select_db ("login", $link) or Die ("Database selection failed". Mysql_error ());
mysql_query ("Set names ' UTF8 '");
$id =$_get[' id '];
$delete = "Delete from title where id= ' $id '";
$query =mysql_query ($delete);
if ($query) {
echo "Delete sucess!";
echo "<meta http-equiv=\" refresh\ "content=\" 1; Url=show_message.php\ "/>";
}
else echo "Delete false";
Change Data
$link =mysql_connect ("localhost", "root", "123456") or Die ("Database connection Failed". Mysql_error ());
$sel =mysql_select_db ("login", $link) or Die ("Database selection failed". Mysql_error ());
mysql_query ("Set names ' UTF8 '", $sel);
$update = "Update title set Title= '". $title. "', content= '". $content. "'
where id= ' $id ';
$query =mysql_query ($update);
if ($query) {
echo "Reset sucess!";
echo "<meta http-equiv=\" refresh\ "content=\" 1; Url=show_message.php\ "/>";
}
else echo "Reset false";
Check Data
$link =mysql_connect ("localhost", "root", "123456") or Die ("Database connection Failed". Mysql_error ());
$sel =mysql_select_db ("login", $link) or Die ("Database selection failed". Mysql_error ());
mysql_query ("Set names ' UTF8 '", $sel);
$sql = "SELECT * FROM table name: Limit 10";
$result =mysql_query ($sql);
while ($arr =mysql_fetch_array ($result, Mysql_assoc)) {
$rs [] = $arr; Put all the data in a two-bit array
or $arr = Mysql_fetch_array (); Take a bar
1. Select Query statements and conditional statements
Selectquery Fields fromTable namewhereconditions
Query fields: Wildcard characters can be used*, field name, field alias (Field nameAs ..)
Table Name: Database.table name, table name
Common conditions:=equals,<>not equal to,inch(value, value) contains, not in(value, value) does not contain, likeMatch
Betweenin the range,Not betweennot in range,<,>
Conditional budget: and,or, ()
2. Selectquery Fields fromTable namewhereconditions
(Group byfield grouping)
(Order byfields, FieldsAsc/descsort)
(Limitinitial value, end value pointer query)
3. SelectCalculation fromTable namewhereconditions
Count(*) Statistical Functions
Max(*) The maximum value function
Min(*) Minimum value function
Avg(*) Average
Sum(*) Accumulated value function
4. Fuzzy query (% represents 0 or more characters, _ represents 1 characters.) Like compare time, try to use less)
Like usage:
SELECT * FROM table name WHERE username like "$username%";
SELECT * FROM table name WHERE username like "% $username%";
In usage:
SELECT * FROM table name WHERE ID in (1, 2, 5, 9);
MySQL additions and deletions to check the basic statement