1. Connection to the database
$DBC = mysql_connect (hosetname, username, password);
2.Mysql Error Handling
Mysql_error (); Detailed reports showing errors
3. Create and select a database
Created: mysql_query (' Create DATABASE somedb ');
Checked: mysql_select_db (' somedb '); Select the database every time you run the query
4. Create a table
$query = ' Create TABLE my_table (id INT PRIMARY KEY, information TEXT); Assign create statement to a variable first
mysql_query ($query);//Add the variable into the mysql_query () function
5. Inserting data
With the CREATE table, each query is assigned a variable, and then the variable is passed to the mysql_query () function:
$query = "INSERT into entries (entry_id, title, entry, data_entered) VALUES (0, ' title ', ' $entry ', Now ())";
mysql_query ($query);
6. Secure query data
For a query entered by the user, use Mysql_real_escape_string ($var) to escape potentially dangerous characters such as single quotes (a backslash will be preceded)
7. Retrieving data from a database
You need to copy the results of the query to a variable:
$query = ' SELECT ' from the users WHERE (name = myname) ';
$result = MySQL ($query);
8. Delete Data
$query = ' DELETE from users WHERE name = myname LIMIT 1 ';
$result = MySQL ($query);
9. Update
$query = UPDATE tablename SET column1 = value, Colunmn2 = value where some_column = value ';
$result = MySQL ($query);
Coding test: ws.php
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
Connect to the database and check if ($dbc = @mysql_connect (' localhost ', ' root ', ') ') {if (@mysql_select_db (' MyData ')) {print ' <p> Selected!</p> ';} Else{print ' <p>can not select Error: '. Mysql_error (). ' </p> ';}} Else{print ' <p>can not connect. Error: '. Mysql_error (). ' </p> ';}
CREATE TABLE/* $create = ' CREATE TABLE myTable (ID INT UNSIGNED NOT NULL auto_increment PRIMARY key,name VARCHAR (+) NOT null) ';p R int "<p>create......</p>", if (@mysql_query ($create)) {print ' <p>created!</p> ';} else {print ' <p>can not create error: '. Mysql_error (). ' </p> ';} Mysql_close (); */
The results show:
PHP Basic Tutorial--5 Database Summary