Table of Contents
php基础教程——5数据库总结
Home php教程 php手册 php基础教程5数据库总结

php基础教程5数据库总结

Jun 13, 2016 am 09:15 AM
Base Tutorial database

php基础教程——5数据库总结

1.数据库的连接

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

2.Mysql错误处理

mysql_error();显示错误的详实报告

3.创建和选择数据库

创建:mysql_query(‘CREATE DATABASE somedb’);

选中:mysql_select_db('somedb'); //每次运行查询前都要选中数据库

4.创建表

$query = 'CREATE TABLE my_table(id INT PRIMARY KEY, information TEXT);将创建语句先赋予一个变量

mysql_query($query);//再将变量放入mysql_query()函数

5.插入数据

同创建表,每个查询都赋予一个变量,然后再将变量传递给mysql_query()函数:

$query = " INSERT INTO entries(entry_id, title, entry, data_entered) VALUES(0, 'title', '$entry', NOW())";

mysql_query($query);

6.安全查询数据

对于用户输入的一段查询,用mysql_real_escape_string($var)对可能危险的字符如单引号,进行转义(将在其前加一个反斜线)

7.从数据库中进行检索数据

需要将查询的结果复制给一个变量:
$query = 'SELECT * FROM users WHERE ( name = myname)';

$result = mysql($query);

8.删除数据

$query = 'DELETE FROM users WHERE name = myname LIMIT 1';

$result = mysql($query);

9.更新

$query = UPDATE tablename SET column1 = value, colunmn2 = value WHERE some_column = value';

$result = mysql($query);

编码测试:ws.php

1

2

3

4

5

6

7

8

9

10

<!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

1

2

3

4

5

6

7

8

9

10

//连接数据库,并选中

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

1

2

3

4

5

6

7

8

9

10

11

12

//创建表

/*

$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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

//插入数据

$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();

 

?>

     

 

<p><p>This is the foot of the document</p></p>

</body>

</html>

Copy after login

结果显示:


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

In summer, you must try shooting a rainbow In summer, you must try shooting a rainbow Jul 21, 2024 pm 05:16 PM

After rain in summer, you can often see a beautiful and magical special weather scene - rainbow. This is also a rare scene that can be encountered in photography, and it is very photogenic. There are several conditions for a rainbow to appear: first, there are enough water droplets in the air, and second, the sun shines at a low angle. Therefore, it is easiest to see a rainbow in the afternoon after the rain has cleared up. However, the formation of a rainbow is greatly affected by weather, light and other conditions, so it generally only lasts for a short period of time, and the best viewing and shooting time is even shorter. So when you encounter a rainbow, how can you properly record it and photograph it with quality? 1. Look for rainbows. In addition to the conditions mentioned above, rainbows usually appear in the direction of sunlight, that is, if the sun shines from west to east, rainbows are more likely to appear in the east.

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

How to retrieve the wrong chain of virtual currency? Tutorial on retrieving the wrong chain of virtual currency transfer How to retrieve the wrong chain of virtual currency? Tutorial on retrieving the wrong chain of virtual currency transfer Jul 16, 2024 pm 09:02 PM

The expansion of the virtual market is inseparable from the circulation of virtual currency, and naturally it is also inseparable from the issue of virtual currency transfers. A common transfer error is the address copy error, and another error is the chain selection error. The transfer of virtual currency to the wrong chain is still a thorny problem, but due to the inexperience of transfer operations, novices often transfer the wrong chain. So how to recover the wrong chain of virtual currency? The wrong link can be retrieved through a third-party platform, but it may not be successful. Next, the editor will tell you in detail to help you better take care of your virtual assets. How to retrieve the wrong chain of virtual currency? The process of retrieving virtual currency transferred to the wrong chain may be complicated and challenging, but by confirming the transfer details, contacting the exchange or wallet provider, importing the private key to a compatible wallet, and using the cross-chain bridge tool

How to connect to remote database using Golang? How to connect to remote database using Golang? Jun 01, 2024 pm 08:31 PM

Through the Go standard library database/sql package, you can connect to remote databases such as MySQL, PostgreSQL or SQLite: create a connection string containing database connection information. Use the sql.Open() function to open a database connection. Perform database operations such as SQL queries and insert operations. Use defer to close the database connection to release resources.

How to use database callback functions in Golang? How to use database callback functions in Golang? Jun 03, 2024 pm 02:20 PM

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

Why do you need to know histograms to learn photography? Why do you need to know histograms to learn photography? Jul 20, 2024 pm 09:20 PM

In daily shooting, many people encounter this situation: the photos on the camera seem to be exposed normally, but after exporting the photos, they find that their true form is far from the camera's rendering, and there is obviously an exposure problem. Affected by environmental light, screen brightness and other factors, this situation is relatively normal, but it also brings us a revelation: when looking at photos and analyzing photos, you must learn to read histograms. So, what is a histogram? Simply understood, a histogram is a display form of the brightness distribution of photo pixels: horizontally, the histogram can be roughly divided into three parts, the left side is the shadow area, the middle is the midtone area, and the right side is the highlight area; On the left is the dead black area in the shadows, while on the far right is the spilled area in the highlights. The vertical axis represents the specific distribution of pixels

See all articles