Hongmu Notes
Home Language Notes PHP connects to the database and determines whether the SQL statement is executed successfully
Language Notes PHP

PHP connects to the database and determines whether the SQL statement is executed successfully

PHP connects to the database and determines whether the SQL statement is executed successfully

I must be getting increasingly lazy—just leave the code here, and I can simply copy it next time I need to use it.

<?php
// 设置数据库连接参数
$servername = "localhost"; // 服务器名
$username = "your_username"; // 用户名
$password = "your_password"; // 密码
$dbname = "your_database_name"; // 数据库名

// 创建数据库连接
$conn = new mysqli($servername, $username, $password, $dbname);

// 检查连接是否成功
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

// 设置要执行的SQL语句
$sql = "INSERT INTO ";

// 执行SQL语句并获取执行结果
if ($conn->query($sql) === TRUE) {
    echo "新记录插入成功";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

// 关闭数据库连接
$conn->close();
?>

 

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

PHP ob function record

PHP ob function record Language Notes PHP

Usage of the following three functions ob_get_contents(); ob_end_clean(); ob_start(); You can use these functions to buffer local files and execute local script code. Use ob_start() to save the output code into the buffer, and the page will not be displayed; then use ob_get_contents to get the data in the buffer. o…
👁 141
PHP preg

PHP preg Language Notes PHP

The preg_match_all function is used to perform a global regular expression match. preg_match_all() Syntax int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG…
👁 169
Detailed explanation of PHP ternary operator and if

Detailed explanation of PHP ternary operator and if Language Notes PHP

Ternary operator condition ? Result 1 : Result 2 Explanation: The position in front of the question mark is the condition for judgment. If the condition is met, the result is 1, and if it is not met, the result is 2. This article compares and explains the ternary operator and if...else... in detail. I hope it will be helpful to everyone. Today when I was revising my paper online, I encountered a statement that I couldn’t understand: $if_summary = $row['IF_SUMMARY']=…
👁 189
PHP cast type

PHP cast type Language Notes PHP PHP collection PHP and mysql

Get the data type 1. If you want to check the value and type of an expression, use var_dump(). 2. If you just want to get an easy-to-read type expression for debugging, use gettype(). 3. To check a certain type, do not use gettype(), but use the is_type() function. Converting Strings to Numbers When a string is evaluated as a number, the result is determined according to the following rules...
👁 232

Recommended reading

(Adaptive mobile version) English-language international e-commerce website template – Download source code for a general merchandise website – 1053

(Adaptive mobile version) English-language international e-commerce website template – Download source code for a general merchandise website – 1053 Practical Collection pbootcms Template

A PbootCMS website template designed for English-language international trade and daily consumer goods businesses, compatible with both PC and WAP devices. Its international and practical design makes it ideal for foreign trade companies to showcase their product ranges and export advantages, helping them promote their brands and products in the global market. Template Overview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn | Related Articles...
👁 56
Responsive Hardware Manufacturing & Sheet Metal Processing Website Template 0610

Responsive Hardware Manufacturing & Sheet Metal Processing Website Template 0610 Practical Collection Yiyou template

This EyouCMS responsive template is ideal for the hardware manufacturing and sheet metal processing industries. Its robust and professional design effectively showcases hardware products, sheet metal processing services, and industrial applications. It helps hardware manufacturing enterprises demonstrate their production capabilities online and attract potential customers. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (...
👁 52
(Adaptive mobile version) PBootCMS website template for business registration agencies; Financial accounting website source code download – 0342

(Adaptive mobile version) PBootCMS website template for business registration agencies; Financial accounting website source code download – 0342 Practical Collection pbootcms Template

This is an adaptive PbootCMS website template designed for business registration and corporate setup services for mobile devices. Its professional and sophisticated design makes it ideal for accounting firms and business registration agencies to showcase their registration processes, financial and tax services, and successful case studies. It helps business service providers attract startup clients online. Template Overview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 39
(Adaptive mobile version) Stone Carving Website Template – Download Stone Processing Website Source Code – 1054

(Adaptive mobile version) Stone Carving Website Template – Download Stone Processing Website Source Code – 1054 Practical Collection pbootcms Template

A PbootCMS website template designed for stone carving and stone processing projects, compatible with both PC and WAP devices. Its professional and artistic design is ideal for showcasing stone carving works, processing techniques, and project examples. This template helps stone-related businesses demonstrate their craftsmanship online and attract clients in the construction and landscaping industries. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.c...
👁 45