| // |
| // Licensed to the Apache Software Foundation (ASF) under one |
| // or more contributor license agreements. See the NOTICE file |
| // distributed with this work for additional information |
| // regarding copyright ownership. The ASF licenses this file |
| // to you under the Apache License, Version 2.0 (the |
| // "License"); you may not use this file except in compliance |
| // with the License. You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, |
| // software distributed under the License is distributed on an |
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| // KIND, either express or implied. See the License for the |
| // specific language governing permissions and limitations |
| // under the License. |
| // |
| |
| = 使用 PHP 创建数据库驱动的应用程序 |
| :jbake-type: tutorial |
| :jbake-tags: tutorials |
| :jbake-status: published |
| :icons: font |
| :syntax: true |
| :source-highlighter: pygments |
| :toc: left |
| :toc-title: |
| :description: 使用 PHP 创建数据库驱动的应用程序 - Apache NetBeans |
| :keywords: Apache NetBeans, Tutorials, 使用 PHP 创建数据库驱动的应用程序 |
| |
| 本课程介绍在开发愿望列表应用程序时的最后一个预备步骤,即,使用测试数据创建样例数据库。要完成本教程中的步骤,您需要创建一个数据库以存储许愿者的数据。使用 NetBeans IDE,您可以通过 IDE 界面执行所有这些活动。 |
| 开始之前,请参见link:wish-list-tutorial-main-page.html[+使用 PHP 创建 CRUD 应用程序 - 主页+]中介绍的教程要求。 |
| |
| 当前文档是“在适用于 PHP 的 NetBeans IDE 中创建 CRUD 应用程序”教程的一部分。 |
| |
| |
| |
| [[register-mysql]] |
| == 注册 MySQL 服务器 |
| |
| 如果您未在 IDE 中注册 MySQL 数据库服务器,或者需要有关结合使用 MySQL 和 NetBeans IDE 的常规信息,请参见link:../ide/mysql.html[+连接到 MySQL 数据库+]。 |
| |
| |
| == 创建数据库用户 |
| |
| 在创建数据库之前,您需要创建一个数据库用户,并向该用户授予对数据库执行任意操作的权限。在创建数据库用户时,您需要执行以下操作: |
| |
| * <<connectToMySQLServer,以 root 用户身份连接到 MySQL 服务器>>。 |
| * <<connectToDefaultDatabase,以 root 用户身份连接到 MySQL 系统数据库>>。必须执行该步骤才能运行 SQL 命令以创建用户,这是因为您在未连接到任何数据库的情况下无法运行 SQL 命令。 |
| * <<createUserQuery,执行用于创建用户的 MySQL 语句>>。 |
| |
| 1. 启动 IDE,切换至 "Services"(服务)窗口(Ctrl-5 组合键),然后展开 "Databases"(数据库)节点。 |
| 2. 要连接到 MySQL 数据库服务器,请导航至 ``MySQL Server`` (MySQL 服务器)节点,然后从上下文菜单中选择 "Connect"(连接)。 |
| |
| image::images/mysql-ctxmenu-connect.png[] |
| |
| |
| [start=3.] |
| . NetBeans IDE 将连接到 MySQL 服务器,检查通过该服务器提供的数据库,检测系统 ``mysql`` 数据库,然后将相应的新节点 ``mysql`` 添加到 "Databases"(数据库)树中。 |
| |
| image::images/mysql_node_added.png[] |
| |
| |
| [start=4.] |
| . 要执行 SQL 命令,您需要连接到一个数据库。由于仅提供了 MySQL 系统,因此,您需要连接到该系统。 |
| 要连接到系统数据库,请导航至 ``mysql`` 节点,然后从上下文菜单中选择 "Connect"(连接)。如果尚未建立连接,则会显示 "New Database Connection"(新建数据库连接)对话框。默认情况下,在 "User Name"(用户名)字段中填写 root。在 "Password"(口令)字段中,输入 root 用户的口令。 |
| |
| NOTE: 如果您以前已连接到 `mysql` 数据库,则此对话框将不显示。而新的连接节点将简单显示在树中。 |
| |
| image::images/create-new-database-connection.png[] |
| |
| "New Database Connection"(新建数据库连接)对话框显示“已建立连接”消息。单击 "OK"(确定)。此时将在 "Databases"(数据库)树中添加一个名为 ``jdbc:mysql://localhost:3306/mysql`` 的新节点。 |
| |
| [start=5.] |
| . 导航至 ``jdbc:mysql://localhost:3306/mysql`` 节点,然后从上下文菜单中选择 "Execute Command"(执行命令)。 |
| |
| image::images/execute-command.png[] |
| |
| 此时将打开一个 "SQL Command"(SQL 命令)窗口。在 "SQL Command"(SQL 命令)窗口中,使用类似于以下语句的语法: |
| |
| [source,sql] |
| ---- |
| |
| CREATE USER 'phpuser'@'localhost' IDENTIFIED BY 'phpuserpw' |
| ---- |
| 从上下文菜单中选择 "Run Statement"(运行语句)。如果成功执行该命令,则状态栏将显示 "SQL Statement(s) executed successfully"(已成功执行 SQL 语句)消息。如果显示其他消息,请检查语法并按照消息提示进行操作。 |
| |
| |
| == 创建 Wishlist 数据库 |
| |
| 创建数据库: |
| |
| 1. 导航至位于 ``localhost:3306 上的 MySQL 服务器`` 节点,然后从上下文菜单中选择 "Create Database"(创建数据库)。此时将显示 "Create MySQL Database"(创建 MySQL 数据库)对话框。填写以下字段: |
| * 在 "Database Name"(数据库名称)字段中,输入 wishlist。 |
| * 选中 "Grant full access to user"(为用户授予完全访问权限)复选框,并从下拉列表中选择 ``phpuser@localhost`` ,然后单击 "OK"(确定)。 |
| |
| image::images/create-user.png[] |
| |
| "Grant full access to user"(为用户授予完全访问权限)功能并非始终正常工作。如果该功能无法正常工作,请以 root 用户身份<<EstablishConnection,连接到数据库>>,然后发送 SQL 查询 [examplecode]# ``GRANT ALL ON wishlist.* TO phpuser@localhost`` #。 |
| |
| 到数据库的连接显示在树中。但是,该连接用于 `root` 用户。您需要一个用于 `phpuser` 用户的连接。 |
| |
| |
| == 建立到 Wishlist 数据库的连接 |
| |
| 在前一部分结束时,您创建了与 `root` 用户连接的 `wishlist` 数据库。现在,您将为 `phpuser` 用户创建一个新连接。 |
| |
| 1. 在 "Services"(服务)窗口中,右键单击 "Databases"(数据库)节点,然后选择 "New Connection"(新建连接)。此时将打开 "New Connection Wizard"(新建连接向导)。 |
| |
| image::images/databases-ctxmenu-newconnection.png[] |
| |
| |
| [start=2.] |
| . 在 "New Connection Wizard"(新建连接向导)的 "Locate Driver"(查找驱动程序)面板中,选择 `MySQL (Connector/J Driver)`(MySQL(Connector/J 驱动程序)),然后单击 "Next"(下一步)。此时将打开 "Customize Connection"(定制连接)面板。 |
| |
| image::images/locate-driver.png[] |
| |
| |
| [start=3.] |
| . 在 "Database"(数据库)字段中,键入 `wishlist`。 |
| |
| |
| [start=4.] |
| . 在 "User Name"(用户名)和 "Password"(口令)编辑框中,输入在<<CreateUser,创建数据库所有者(用户)>>部分指定的名称和口令(在我们的示例中,分别是 ``phpuser`` 和 ``phpuserpw`` )。单击 "Remember Password"(记住口令)。单击 "Test Connection"(测试连接),如果连接成功,则单击 "OK"(确定)。 |
| |
| image::images/phpuser-connection.png[] |
| |
| 此时将在 "Databases"(数据库)树中显示相应的新连接节点。现在,您可以删除 `root` 用户与 `wishlist` 数据库的连接。单击 ``jdbc:mysql://localhost:3306/wishlist [root on Default schema]`` 连接并选择 "Delete"(删除)。 |
| |
| image::images/new-database-connection-added.png[] |
| |
| |
| == 设计 Wishlist 数据库的结构 |
| |
| 要排列和存储所有必需数据,您需要使用两个表: |
| |
| * 一个是 wishers 表,用于存储注册用户的名称和口令 |
| * 一个是 wishes 表,用于存储愿望说明 |
| |
| image::images/wishlist-db.png[] |
| |
| wishers 表包含三个字段: |
| |
| 1. id - 许愿者的唯一 ID。该字段用作主键 |
| 2. name |
| 3. password |
| |
| wishes 表包含四个字段: |
| |
| 1. id - 愿望的唯一 ID。该字段用作主键 |
| 2. wisher_id - 愿望所属的许愿者的 ID。该字段用作外键。 |
| 3. description |
| 4. due_date - 请求愿望时的日期 |
| |
| 这些表通过许愿者的 ID 相关联。除了 wishes 表中的 due_date 以外,所有字段都是必填的。 |
| |
| |
| == 创建表 |
| |
| 1. 要连接到数据库,请在 ``jdbc:mysql://localhost:3306/wishlist`` 连接上单击鼠标右键,然后从上下文菜单中选择 "Connect"(连接)。 |
| NOTE: 如果禁用了该菜单项,则说明您已建立了连接。继续执行步骤 2。 |
| |
| [start=2.] |
| . 从上述上下文菜单中选择 "Execute Command"(执行命令)。此时将打开一个空的 "SQL Command"(SQL 命令)窗口。 |
| |
| [start=3.] |
| . 创建 wishers 表: |
| |
| [start=1.] |
| . 键入以下 SQL 查询(请注意,您需要将字符集明确设置为 UTF-8 以实现国际化): |
| |
| [source,sql] |
| ---- |
| |
| CREATE TABLE wishers(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name CHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL UNIQUE,password CHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL) |
| ---- |
| NOTE: 可通过为某个字段指定 AUTO_INCREMENT 属性,从 MySQL 中获取自动生成的唯一数字。MySQL 会通过增加表的最后一个数字来生成一个唯一数字,并自动将其添加到自动递增的字段中。在我们的示例中,"ID" 字段是自动递增的。 |
| |
| [start=2.] |
| . 在查询上单击鼠标右键,然后从上下文菜单中选择 "Run Statement"(运行语句)。 |
| |
| NOTE: MySQL 的默认存储引擎是 MyISAM,它不支持外键。如果要使用外键,请考虑将 InnoDB 用作存储引擎。 |
| |
| |
| [start=4.] |
| . 创建 wishes 表: |
| |
| [start=1.] |
| . 键入以下 SQL 查询: |
| |
| [source,sql] |
| ---- |
| |
| CREATE TABLE wishes(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,wisher_id INT NOT NULL,description CHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,due_date DATE,FOREIGN KEY (wisher_id) REFERENCES wishers(id)) |
| ---- |
| |
| [start=2.] |
| . 在查询上单击鼠标右键,然后从上下文菜单中选择 "Run Statement"(运行语句)。 |
| |
| [start=5.] |
| . 要验证新表是否添加到数据库中,请切换至 "Services"(服务)窗口,然后导航至 jdbc:mysql://localhost:3306/wishlist 连接节点。 |
| |
| [start=6.] |
| . 单击鼠标右键,然后选择 "Refresh"(刷新)。此时将在树中显示 wishers 和 wishes 节点。 |
| |
| 注:您可以在link:https://netbeans.org/projects/www/downloads/download/php%252FSQL-files-for-MySQL.zip[+此处+]下载一组 SQL 命令以创建 MySQL wishlist 数据库。 |
| |
| |
| == 输入测试数据 |
| |
| 要测试应用程序,您需要使用数据库中的某些数据。下面的示例说明了如何添加两个许愿者和四个愿望。 |
| |
| |
| [start=1.] |
| . 在 jdbc:mysql://localhost:3306/wishlist 连接上单击鼠标右键,然后选择 "Execute Command"(执行命令)。此时将打开一个空的 "SQL Command"(SQL 命令)窗口。 |
| |
| [start=2.] |
| . 要添加许愿者,请使用类似下面示例的语法: |
| |
| [source,sql] |
| ---- |
| |
| INSERT INTO wishers (name, password) VALUES ('Tom', 'tomcat'); |
| ---- |
| 在查询上单击鼠标右键,然后从上下文菜单中选择 "Run Statement"(运行语句)。 |
| NOTE: 语句不包含 ``id`` 字段的值。此时将会自动输入值,因为字段类型已指定为 ``AUTO_INCREMENT`` 。 |
| 输入另一个测试许愿者: |
| |
| [source,sql] |
| ---- |
| |
| INSERT INTO wishers (name, password) VALUES ('Jerry', 'jerrymouse'); |
| ---- |
| |
| [start=3.] |
| . 要添加愿望,请使用类似下面示例的语法: |
| |
| [source,sql] |
| ---- |
| |
| INSERT INTO wishes (wisher_id, description, due_date) VALUES (1, 'Sausage', 080401);INSERT INTO wishes (wisher_id, description) VALUES (1, 'Icecream');INSERT INTO wishes (wisher_id, description, due_date) VALUES (2, 'Cheese', 080501);INSERT INTO wishes (wisher_id, description)VALUES (2, 'Candle'); |
| ---- |
| |
| 选择查询,在每个查询上单击鼠标右键,然后从上下文菜单中选择 "Run Selection"(运行选择)。 |
| |
| NOTE: 您还可以依次执行查询,如第 2 项所述。 |
| |
| |
| [start=4.] |
| . 要查看测试数据,请在相关表上单击鼠标右键,然后从上下文菜单中选择 "View Data"(查看数据)。 |
| |
| image::images/view-test-data.png[] |
| |
| 要大致了解数据库原理和设计模式,请查阅以下教程:link:http://www.tekstenuitleg.net/en/articles/database_design_tutorial/1[+http://www.tekstenuitleg.net/en/articles/database_design_tutorial/1+]。 |
| |
| 有关 MySQL ``CREATE TABLE`` 语句语法的详细信息,请参见 link:http://dev.mysql.com/doc/refman/5.0/en/create-table.html[+http://dev.mysql.com/doc/refman/5.0/en/create-table.html+]。 |
| |
| 有关在表中插入值的详细信息,请参见 link:http://dev.mysql.com/doc/refman/5.0/en/insert.html[+http://dev.mysql.com/doc/refman/5.0/en/insert.html+]。 |
| |
| 注:您可以在link:https://netbeans.org/projects/www/downloads/download/php%252FSQL-files-for-MySQL.zip[+此处+]下载一组 SQL 命令以创建 MySQL wishlist 数据库。 |
| |
| |
| == 后续步骤 |
| |
| link:wish-list-lesson2.html[+下一课 >>+] |
| |
| link:wish-list-tutorial-main-page.html[+返回到教程主页+] |
| |
| |
| link:/about/contact_form.html?to=3&subject=Feedback:%20PHP%20Wish%20List%20CRUD%201:%20Create%20MySQL%20Database[+请将您的反馈意见发送给我们+] |
| |
| |
| 要发送意见和建议、获得支持以及随时了解 NetBeans IDE PHP 开发功能的最新开发情况,请link:../../../community/lists/top.html[+加入 users@php.netbeans.org 邮件列表+]。 |
| |
| link:../../trails/php.html[+返回至 PHP 学习资源+] |
| |