Home Database Mysql Tutorial 新手学PHP和MySQL动态网站开发教程(1)_MySQL

新手学PHP和MySQL动态网站开发教程(1)_MySQL

Jun 01, 2016 pm 01:57 PM
windows dynamic Tutorial

    提示:从入门到精通---初学PHP 和 MySQL


  这是一个基本的教程。没有怪异的代码,只是一些基础。现在有大量的教程是基于UNIX机器的,这个教程将集中在基于Windows平台上。然而,除了安装部分,有或多或少的针对于Windows的说明外,其它部分对所有的平台都是一样的。顺便说一下,关于安装部分,请看本站的安装指南。在这个教程中,我们将一步一步地建立一个小的网站,使用了PHP和MySQL的下面特性:

  1. 查看数据库;
  2. 编辑数据库的记录;
  3. 修改数据库的记录;
  4. 删除数据库的记录。
  
  我们将同时学习MySQL和PHP,一起去感觉它们。本文直接从这里学,如果不会安装配置Apache+PHP+Mysql请查看网页教学网相关文章吧!

  先运行web服务器(已增加PHP扩展); 运行MySQL。
  
  创建和操纵一个MySQL数据库:
  
  首先我们需要创建要使用的数据库和表。数据库起名为"example",表名为"tbl",有以下字段:识别号,名,姓和信息。要通过mysql终端完成建库和定义表的工作,只要双击或运行c:/mysql/bin/mysql.exe。
  如果要看在MySQL中已经定义了哪些表,可以使用(注意mysql>是终端提示符):
  Mysql> show databases;
  这个命令可能显示如下信息:
  +----------+
  | Database |
  +----------+
  | mysql |
  | test |
  +----------+
  2 rows in set (0.01 sec)
  为了定义一个新的数据库(example),键入:
  Mysql> create database example;
  你将会看到一个回答,如:
  Query OK, 1 row affected (0.17 sec)很发,我们现在有了一个新数据库了。现在我们可以在库中建立一个新表,但首先我们需要先选中新的数据库:
  Mysql> use example;
  
  回答应该是:
  Database changed
  现在我们可以建表了,有如下字段:
  
  索引号 - 整数
  用户名 - 最大长度为30的字符串
  用户姓 - 最大长度为50的字符串
  自由信息 - 最大长度为100的字符串
  在MySQL提示符下键入下面的命令来创建表:
  MySQL> create table tbl (idx integer(3), UserName varchar(30), LastName varchar(50), FreeText varchar(100));
  
  回答应该是:
  Query OK, 0 rows affected (0.01 sec)
  好,让我们看一下从MySQL提示符下看表是什么样子的,键入命令:
  MySQL> show columns from tbl;
  我们将得到下面的结果:

以下为引用的内容:
  +----------+--------------+------+-----+---------+-------+
  | Field | Type | Null | Key | Default | Extra |
  +----------+--------------+------+-----+---------+-------+
  | idx | int(3) | YES | | NULL | |
  | UserName | varchar(30) | YES | | NULL | |
  | LastName | varchar(50) | YES | | NULL | |
  | FreeText | varchar(100) | YES | | NULL | |
  +----------+--------------+------+-----+---------+-------+


  4 rows in set (0.00 sec)
  在这里,我们可以看到刚创建的表"tbl"的内容。
  
  现在让我们看一下表中有什么内容。键入下面的命令:
  MySQL> select * from tbl;
  
  这个命令是用来显示表"tbl"中的所有数据的。输出可能是:
  Empty set (0.07 sec)之所以得到这个结果,是因为我们还没有在表中插入任何数据。让我们往表中插入一些数据,键入:
  MySQL> insert into tbl values (1,'Rafi','Ton','Just a test');
  
  Query OK, 1 row affected (0.04 sec)
  如上所见,我们插入到表中的值是按照前面我们定义表的顺序,因为使用的是缺省的顺序。我们可以设定数据的顺序,语法如下:
  MySQL> insert into tbl (idx,UserName,LastName,FreeText) values (1,'Rafi','Ton','Just a test');
  
  好,现在我们可以再看一下表中的内容:
  
  MySQL> select * from tbl;
  这次的结果是:
  +------+----------+----------+-------------+
  | idx | UserName | LastName | FreeText |
  +------+----------+----------+-------------+
  | 1 | Rafi | Ton | Just a test |
  +------+----------+----------+-------------+
  1 row in set (0.00 sec)
  现在我们可以看到表的结构和每一个单元格的内容。
  
  现在我们想删除数据。为了实现我们应该键入:
  
  MySQL> delete from tbl where idx=1 limit 1; Query OK, 1 row affected (0.00 sec)
  
  好,给出一些解释。我们正在告诉MySQL从"tbl"表中删除记录,删除那些idx字段值为1的记录,并且只限制删除一条记录。如果我们不限制删除记录数为1,那么所有idx为1的记录都将被删除(在这个例子中我们只有一条记录,但是虽然如此,我只是想让这一点更加清楚)。
  
  不幸的是,我们又一次得到了一个空表,所以让我们再输进去:
  
以下为引用的内容:
  MySQL> insert into tbl values (1,'Rafi','Ton','Just a test');
  Query OK, 1 row affected (0.04 sec)

  另一件可以做的事是,修改指定字段的内容,使用"update"命令:

以下为引用的内容:
  MySQL>update tbl set UserName='Berber' where UserName='Rafi';
  Query OK, 1 row affected (0.01 sec)
  Rows matched: 1 Changed: 1 Warnings: 0


  这个命令将搜索所有UserName为"Rafi"的记录,并将它改为"Berber"。注意,set部分和where部分不一定要一样。我们可以索搜一个字段但是改变另一个字段。而且,我们可以执行两个或更多条件的搜索。
  
以下为引用的内容:
  MySQL>update tbl set UserName='Rafi' where UserName='Berber' and LastName='Ton';
  Query OK, 1 row affected (0.04 sec)

  这个查询搜索了两个字段,改变了UserName的值

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Can I install mysql on Windows 7 Can I install mysql on Windows 7 Apr 08, 2025 pm 03:21 PM

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

How to speed up the loading speed of PS? How to speed up the loading speed of PS? Apr 06, 2025 pm 06:27 PM

Solving the problem of slow Photoshop startup requires a multi-pronged approach, including: upgrading hardware (memory, solid-state drive, CPU); uninstalling outdated or incompatible plug-ins; cleaning up system garbage and excessive background programs regularly; closing irrelevant programs with caution; avoiding opening a large number of files during startup.

How to distinguish between closing a browser tab and closing the entire browser using JavaScript? How to distinguish between closing a browser tab and closing the entire browser using JavaScript? Apr 04, 2025 pm 10:21 PM

How to distinguish between closing tabs and closing entire browser using JavaScript on your browser? During the daily use of the browser, users may...

How to pull the vertical reference line of PS How to pull the vertical reference line of PS Apr 06, 2025 pm 08:18 PM

Pull vertical guides in Photoshop: Enable ruler view (View > ruler). Hover the mouse over the vertical edge of the ruler, and then the cursor becomes a vertical line with double arrows and hold and drag the mouse to pull out the reference line. Click Delete by dragging the guide, or hovering it into a cross.

Solutions to the errors reported by MySQL on a specific system version Solutions to the errors reported by MySQL on a specific system version Apr 08, 2025 am 11:54 AM

The solution to MySQL installation error is: 1. Carefully check the system environment to ensure that the MySQL dependency library requirements are met. Different operating systems and version requirements are different; 2. Carefully read the error message and take corresponding measures according to prompts (such as missing library files or insufficient permissions), such as installing dependencies or using sudo commands; 3. If necessary, try to install the source code and carefully check the compilation log, but this requires a certain amount of Linux knowledge and experience. The key to ultimately solving the problem is to carefully check the system environment and error information, and refer to the official documents.

【Rust Self-study】Introduction 【Rust Self-study】Introduction Apr 04, 2025 am 08:03 AM

1.0.1 Preface This project (including code and comments) was recorded during my self-taught Rust. There may be inaccurate or unclear statements, please apologize. If you benefit from it, it's even better. 1.0.2 Why is RustRust reliable and efficient? Rust can replace C and C, with similar performance but higher security, and does not require frequent recompilation to check for errors like C and C. The main advantages include: memory security (preventing null pointers from dereferences, dangling pointers, and data contention). Thread-safe (make sure multi-threaded code is safe before execution). Avoid undefined behavior (e.g., array out of bounds, uninitialized variables, or access to freed memory). Rust provides modern language features such as generics

MySQL can't be installed after downloading MySQL can't be installed after downloading Apr 08, 2025 am 11:24 AM

The main reasons for MySQL installation failure are: 1. Permission issues, you need to run as an administrator or use the sudo command; 2. Dependencies are missing, and you need to install relevant development packages; 3. Port conflicts, you need to close the program that occupies port 3306 or modify the configuration file; 4. The installation package is corrupt, you need to download and verify the integrity; 5. The environment variable is incorrectly configured, and the environment variables must be correctly configured according to the operating system. Solve these problems and carefully check each step to successfully install MySQL.

See all articles