PHP basic tutorial - 5 database summary_PHP tutorial

WBOY
Release: 2016-07-13 10:07:59
Original
995 people have browsed it

php basic tutorial - 5 database summary

1. Database connection

$dbc = mysql_connect(hosetname, username, password);

2.Mysql error handling

mysql_error(); displays a detailed report of the error

3. Create and select database

Create: mysql_query(‘CREATE DATABASE somedb’);

Select: mysql_select_db('somedb'); //The database must be selected before each query is run

4. Create table

$query = 'CREATE TABLE my_table(id INT PRIMARY KEY, information TEXT); assign the create statement to a variable first

mysql_query($query);//Put the variable into the mysql_query() function

5. Insert data

Same as creating a 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 data query

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 added in front of them)

7. Retrieve data from the database

You need to copy the query results 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);

Coding test: ws.php

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>test</title><base> 
</head> 
<body> 


<?php
Copy after login
//连接数据库,并选中
if ($dbc = @mysql_connect(&#39;localhost&#39;, &#39;root&#39;, &#39;&#39;)){
	if (@mysql_select_db(&#39;mydata&#39;)){
		print &#39;<p>selected!</p>&#39;;
	}else{
		print &#39;<p>can not select error: &#39;. mysql_error().&#39;</p>&#39;;
	}
}else{
	print &#39;<p>can not connect. error: &#39;. mysql_error().&#39;</p>&#39;;
}
Copy after login
//创建表
/*
$create = &#39;CREATE TABLE myTable(
		id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
		name VARCHAR(100) NOT NULL
	)&#39;;print "<p>create……</p>";
	
	if (@mysql_query($create)){
		print &#39;<p>created!</p>&#39;;
	}else {
		print &#39;<p>can not create error: &#39;. mysql_error().&#39;</p>&#39;;
	}mysql_close();*/
Copy after login
//插入数据
$insert = &#39;INSERT INTO myTable (id, name) VALUES (12345, "charles")&#39;;
if (@mysql_query($insert)){
		print &#39;<p>inserted!</p>&#39;;
	}else {
		print &#39;<p>can not insert error: &#39;. mysql_error().&#39;</p>&#39;;
	}mysql_close();

?>
	

<div><p>This is the foot of the document</p></div>
</body> 
</html> 
Copy after login

The result shows:


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/953325.htmlTechArticlephp basic tutorial - 5 database summary 1. Database connection $dbc = mysql_connect(hosetname, username, password) ; 2.Mysql error handling mysql_error(); Display detailed error report 3. Create...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template