PHP Basics Tutorial--5 Database Summary

Source: Internet
Author: User
Tags error handling insert key connect mysql php basics query variable

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:




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.