1. Connection to the database
$DBC = mysql_connect (hosetname, username, password);
2.Mysql Error Handling
Mysql_error (); Informative report showing errors
3. Create and select a database
Create: mysql_query (' Create DATABASE somedb ');
Checked: mysql_select_db ("Somedb"); Check the database before you run the query
4. Create a table
$query = "Create TABLE my_table (id INT PRIMARY KEY, information TEXT); Assign the creation statement first to a variable
mysql_query ($query);//Then put the variable into the mysql_query () function
5. Inserting data
With the CREATE table, each query assigns a variable and then passes the variable 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 a user, use Mysql_real_escape_string ($var) to escape a potentially dangerous character such as a single quotation mark (which will be preceded by a backslash)
7. Retrieving data from a database
You need to copy the results of the query to a variable:
$query = "SELECT * from 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);
Coded test: ws.php
<title>Test</title>
-->Connect to the database and select
if ($dbc = @mysql_connect (' localhost ', ' root ', ')} {
if (@mysql_select_db (' MyData ')) {
print 'selected!
';
} else{
print 'Can not select error: '. Mysql_error (). '
';
}
} else{
print 'Can not connect. Error: '. Mysql_error (). '
';
}
Creating table
/* $create = ' CREATE TABLE myTable (
ID INT UNSIGNED not NULL auto_increment PRIMARY KEY,
name Varcha R () not NULL
) ';p rint 'Create ...
";
if (@mysql_query ($create)) {
print 'created!
';
} else {
print 'Can not create error: '. Mysql_error (). '
';
} Mysql_close ();
Insert data
$insert = ' INSERT into myTable (ID, name) VALUES (12345, "Charles") ";
if (@mysql_query ($insert)) {
print 'inserted!
';
} else {
print 'Can not insert error: '. Mysql_error (). '
';
} Mysql_close ();
? >
This is the foot of the document
The results show: