PHP Basic Tutorial--5 Database Summary

Source: Internet
Author: User

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

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.