PHP MySQL入门教程之创建数据库/表及保存数据
在mysql中如果我们想利用PHP创建数据库和表方法很简单,我们可以直接使用mysql_query()来执行mysql的创建表命令,CREATE DATABASE database_name即可。
具体如下
为了让 PHP 执行上面的语句,我们必须使用 mysql_query() 函数。此函数用于向 MySQL 连接发送查询或命令。
例子
在下面的例子中,我们创建了一个名为 “my_db” 的数据库:
代码如下 | 复制代码 |
$con = mysql_connect("localhost","peter","abc123"); |
创建表
CREATE TABLE 用于在 MySQL 中创建数据库表。
例子
下面的例子展示了如何创建一个名为 “Persons” 的表,此表有三列。列名是 “FirstName”, “LastName” 以及 “Age”:
代码如下 | 复制代码 |
$con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } // Create database if (mysql_query("CREATE DATABASE my_db",$con)) { echo "Database created"; } else { echo "Error creating database: " . mysql_error(); } // Create table in my_db database mysql_select_db("my_db", $con); $sql = "CREATE TABLE Persons ( FirstName varchar(15), LastName varchar(15), Age int )"; mysql_query($sql,$con); mysql_close($con); ?> |
向数据库表插入数据
您还可以规定希望在其中插入数据的列:
INSERT INTO table_name (column1, column2,...)
VALUES (value1, value2,....)
例子
在前面的章节,我们创建了一个名为 “Persons” 的表,有三个列:”Firstname”, “Lastname” 以及 “Age”。我们将在本例中使用同样的表。下面的例子向 “Persons” 表添加了两个新记录:
代码如下 | 复制代码 |
$con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); mysql_query("INSERT INTO Persons (FirstName, LastName, Age) VALUES ('Peter', 'Griffin', '35')"); mysql_query("INSERT INTO Persons (FirstName, LastName, Age) VALUES ('Glenn', 'Quagmire', '33')"); mysql_close($con); ?> |
--------------------------------------------------------------------------------
把来自表单的数据插入数据库
代码如下 | 复制代码 |
|
当用户点击上例中 HTML 表单中的提交按钮时,表单数据被发送到 “insert.php”。”insert.php” 文件连接数据库,并通过 $_POST 变量从表单取回值。然后,mysql_query() 函数执行 INSERT INTO 语句,一条新的记录会添加到数据库表中。
下面是 “insert.php” 页面的代码:
代码如下 | 复制代码 |
$con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); $sql="INSERT INTO Persons (FirstName, LastName, Age) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"; mysql_close($con) ?> |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools