Hongmu Notes
Home Language Notes PHP – Query a single SQL record
Language Notes PHP PHP and mysql

PHP – Query a single SQL record

PHP – Query a single SQL record

You can use the mysqli or PDO extensions in PHP to query a single SQL record. Below are example codes for both methods:

Use the mysqli extension to query a single SQL record.

// 创建连接
$conn = mysqli_connect("localhost", "username", "password", "dbname");

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

// 执行查询
$sql = "SELECT * FROM mytable WHERE id = 1";
$result = mysqli_query($conn, $sql);

// 获取结果
if (mysqli_num_rows($result) > 0) {
    $row = mysqli_fetch_assoc($result);
    // 输出结果
    echo "id: " . $row["id"] . " - 名称: " . $row["name"] . " - 描述: " . $row["description"];
} else {
    echo "没有找到数据";
}

// 关闭连接
mysqli_close($conn);

Use the PDO extension to query a single SQL record:

// 创建连接
$dsn = "mysql:host=localhost;dbname=mydb;charset=utf8mb4";
$username = "username";
$password = "password";

try {
    $conn = new PDO($dsn, $username, $password);
    // 设置错误模式为异常
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo "连接失败: " . $e->getMessage();
}

// 执行查询
$sql = "SELECT * FROM mytable WHERE id = 1";
$stmt = $conn->query($sql);

// 获取结果
if ($stmt->rowCount() > 0) {
    $row = $stmt->fetch();
    // 输出结果
    echo "id: " . $row["id"] . " - 名称: " . $row["name"] . " - 描述: " . $row["description"];
} else {
    echo "没有找到数据";
}

// 关闭连接
$conn = null;

This sample code assumes that the table to be queried is named "mytable", which contains three columns: "id", "name", and "description". In these examples, the query condition is `id = 1`; you can modify both the query condition and the way the output is formatted according to your specific requirements.

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

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

Author homepage View home page →

Related articles

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
PHP special character escaping and restoration

PHP special character escaping and restoration Language Notes PHP PHP collection PHP and mysql

Escape character is a special character constant. Escape characters are backslashed " & quot; At the beginning, followed by one or more characters. The escaped character has a specific meaning, which is different from the original meaning of the character, so it is called "escaped" character. The use of escape characters 1: turn ordinary characters into special purposes, such as back key and enter key. 2. Used to convert a character with special meaning back to its original meaning. 3. Before data is written into the database, escape characters (function …
👁 180
mysqli in PHP

mysqli in PHP Language Notes PHP PHP collection PHP and mysql

The `mysqli_num_rows()` function is exclusively used with `SELECT` query methods, whereas the `mysqli_affected_rows()` function returns the number of rows affected by the previous SQL statement across the entire database; this function is primarily used with `INSERT`, `UPDATE`, and `DELETE` operations.
👁 145

Recommended reading

(PC+WAP) Blue Marketing-oriented Fiber Molding Industry Equipment – pbootcms website template; Download source code for pulp molding carbon fiber machinery website – 0648

(PC+WAP) Blue Marketing-oriented Fiber Molding Industry Equipment – pbootcms website template; Download source code for pulp molding carbon fiber machinery website – 0648 Practical Collection pbootcms Template

A blue marketing-oriented fiber forming equipment and pulp-molded carbon fiber machine website template (PbootCMS), compatible with both PC and WAP devices. The professional, tech-driven design effectively showcases advanced fiber forming technologies and equipment. It helps specialized machinery manufacturing enterprises demonstrate their technical expertise online and attract industry clients. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password:...
👁 62
EmpireCMS – Quickly modify data titles and title images when creating a clone site, making testing easier.

EmpireCMS – Quickly modify data titles and title images when creating a clone site, making testing easier. Program Notes Empire cms

Modified SQL code for desired effect 🔐 Hidden content: View after commenting. `UPDATE `phome_ecms_news` SET `title` = CONCAT('This is a test article; the article ID is', id, ',', 'Please modify the article in the backend section if needed');` – Quick batch thumbnail setup: Thumbs are configured directly using PHP. PHP code: Randomly retrieve files from a specified folder...
👁 639
Responsive Federation of Industry and Commerce Website Template 1148

Responsive Federation of Industry and Commerce Website Template 1148 Practical Collection Yiyou template

This EyouCMS responsive template is ideal for industry and commerce federations as well as various associations. Its professional and sophisticated design effectively showcases an association's functions, member services, industry trends, and corporate profile. It helps industry associations establish an online information exchange platform. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (EyouC...
👁 56
Firefighting and Security Equipment Website Template 0299

Firefighting and Security Equipment Website Template 0299 Practical Collection Yiyou template

This EyouCMS template is ideal for the fire safety, firefighting, and security products industry. Its eye-catching and professional design effectively showcases fire safety products, security equipment, security solutions, and corporate strength. It enables security and fire safety companies to showcase their products online, attracting both corporate and government procurement opportunities. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS...
👁 54
(Adaptive mobile version) HTML5 responsive eco-friendly wastewater treatment equipment PBootCMS website template; Vacuum pump equipment website source code download – 0649

(Adaptive mobile version) HTML5 responsive eco-friendly wastewater treatment equipment PBootCMS website template; Vacuum pump equipment website source code download – 0649 Practical Collection pbootcms Template

A responsive, eco-friendly wastewater treatment equipment and vacuum pump system – paired with a PbootCMS website template compatible with both PC and WAP devices. The modern, professional design is ideal for showcasing wastewater treatment technologies, equipment products, and project cases. This template helps environmental equipment companies present their solutions online and attract municipal and industrial clients. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password:...
👁 33
Responsive Environmental Protection Association Website Template 1149

Responsive Environmental Protection Association Website Template 1149 Practical Collection Yiyou template

An EYO CMS responsive website template designed specifically for environmental protection associations. Its eco-friendly design effectively showcases the association's functions, industry trends, member services, and environmental initiatives, helping the association establish an online platform for industry exchange. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EYO CMS | EYO CMS (Eyo...
👁 45