当前位置:网站首页>PHP MySQL inserts data
PHP MySQL inserts data
2022-07-03 17:51:00 【Crooning ~ shallow singing】
Use MySQLi and PDO towards MySQL insert data
After creating the database and tables , We can add data to the table .
Here are some grammar rules :
- PHP in SQL Query statements must use quotation marks
- stay SQL String values in query statements must be quoted
- The value of a numeric value does not need quotation marks
- NULL Values don't need quotes
INSERT INTO Statements are usually used to direct MySQL Table add new record :
INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)
Learn more about SQL knowledge , Please check our SQL course .
We have created tables in the previous chapters "MyGuests", Table fields have : "id", "firstname", "lastname", "email" and "reg_date". Now? , Let's start filling the table with data .
| Be careful : If the column is set AUTO_INCREMENT ( Such as "id" Column ) or TIMESTAMP ( Such as "reg_date" Column ),, We don't need to be in SQL Specify a value in the query statement ; MySQL Automatically adds a value to the column . |
|---|
The following example shows "MyGuests" A new record has been added to the table :
example (MySQLi - object-oriented )
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Detection connection if ($conn->connect_error) { die(" The connection fails : " . $conn->connect_error); } $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', '[email protected]ample.com')"; if ($conn->query($sql) === TRUE) { echo " New record inserted successfully "; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); ?>
example (MySQLi - Process oriented )
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // Create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // Detection connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', '[email protected]')"; if (mysqli_query($conn, $sql)) { echo " New record inserted successfully "; } else { echo "Error: " . $sql . "<br>" . mysqli_error($conn); } mysqli_close($conn); ?>
example (PDO)
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDBPDO"; try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); // Set up PDO Wrong mode , Used to throw an exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', '[email protected]')"; // Use exec() , No results returned $conn->exec($sql); echo " New record inserted successfully "; } catch(PDOException $e) { echo $sql . "<br>" . $e->getMessage(); } $conn = null; ?>
边栏推荐
- First day of rhcsa study
- 聊聊支付流程的设计与实现逻辑
- Hongmeng fourth training
- [combinatorics] recursive equation (special solution form | special solution solving method | special solution example)
- Interviewer: why is the value nil not equal to nil?
- Select 3 fcpx plug-ins. Come and see if you like them
- STM32H7 HAL库SPI DMA发送一直处于busy的解决办法
- Talk about the design and implementation logic of payment process
- [Yu Yue education] family education SPOC class 2 reference materials of Shanghai Normal University
- As soon as we enter "remote", we will never regret, and several people will be happy and several people will be sad| Community essay solicitation
猜你喜欢

Talk about the design and implementation logic of payment process

互联网医院HIS管理平台源码,在线问诊,预约挂号 智慧医院小程序源码

基于人脸识别的课堂考勤系统 tkinter+openpyxl+face_recognition

Draw some simple graphics with MFC

Play with fancy special effects. This AE super kit is for you

Internet hospital his management platform source code, online consultation, appointment registration smart hospital applet source code

Select 3 fcpx plug-ins. Come and see if you like them

一入“远程”终不悔,几人欢喜几人愁。| 社区征文
![[UE4] brush Arctic pack high quality Arctic terrain pack](/img/e7/bc86bd8450b0b2bdec8980a2aa1a10.jpg)
[UE4] brush Arctic pack high quality Arctic terrain pack

UE4 official charging resources, with a total price of several thousand
随机推荐
Where is the monitoring page of RDS database?
Internet Hospital his Management Platform source, online Inquiry, appointment Registration Smart Hospital Small program source
Servlet specification Part II
Five problems of database operation in commodity supermarket system
link preload prefetch
Type conversion, variable
Write a program to process a list container of string type. Find a special value in the container 9.27: and delete it if found. Rewrite the above procedure with deque container.
1164 Good in C
微服务组件Sentinel控制台调用
ArrayList分析3 : 删除元素
小程序 多tab 多swiper + 每个tab分页
PHP processing - watermark images (text, etc.)
Design limitations of structure type (struct)
STM32H7 HAL库SPI DMA发送一直处于busy的解决办法
Postfix tips and troubleshooting commands
STM32实现74HC595控制
How to deploy applications on kubernetes cluster
Keepalived 设置不抢占资源
[Yu Yue education] family education SPOC class 2 reference materials of Shanghai Normal University
Collection of the most beautiful graduation photos in the graduation season, collection of excellent graduation photos
