使用 XAMPP 从本地主机服务器数据库获取数据的 PHP 程序

王林
发布: 2024-08-28 12:01:10
原创
763 人浏览过

XAMPP是什么?

XAMPP 是一个软件包,使用户能够在自己的计算机上创建本地 Web 开发环境。它包括 Apache Web 服务器、MySQL 数据库、PHP 脚本语言和 Perl 编程语言。 XAMPP 简化了设置用于测试和开发 Web 应用程序的 Web 服务器的过程,允许用户离线处理他们的项目。它被开发人员广泛用于在将网站或 Web 应用程序部署到实时服务器之前对其进行原型设计和调试。

什么是数据库?

数据库是在计算机系统中组织和存储的结构化数据集合。它充当以结构化方式存储和管理大量信息的中央存储库,使检索、操作和分析数据变得容易。数据库在各种应用程序和行业中用于存储客户信息、产品详细信息、财务记录等数据。它们提供了一种在表中存储数据的结构化方法,每个表都由行和列组成。数据库使用查询语言,例如 SQL(结构化查询语言)来执行创建、读取、更新和删除数据等操作。

从本地主机服务器数据库获取数据

按照步骤从服务器获取数据

启动 XAMPP:要打开 XAMPP 服务器,请按照以下步骤操作

第1步:启动XAMPP服务器

  • 启动 XAMPP 控制面板。

  • 单击每个服务旁边的“启动”按钮启动 Apache 和 MySQL 服务。

PHP program to Fetch Data from Localhost Server Database using XAMPP

第2步:访问phpMyAdmin

  • 打开网络浏览器并转到http://localhost/phpmyadmin。

  • phpMyAdmin 是一个基于 Web 的应用程序,用于管理 MySQL 数据库。

PHP program to Fetch Data from Localhost Server Database using XAMPP

创建数据库:先创建数据库,然后在MySQL中创建表,步骤如下。

第一步:创建数据库

  • 在 phpMyAdmin 中,单击“数据库”选项卡。

  • 在“创建数据库”字段中输入数据库的名称。

  • 点击“创建”按钮创建数据库。

  • 这里我使用“Assignments.txt”创建了数据库。

PHP program to Fetch Data from Localhost Server Database using XAMPP

第2步:创建表

  • 从左侧边栏中选择新创建的数据库。

  • 单击“SQL”选项卡。

  • 输入以下 SQL 查询来创建表:

  • PHP program to Fetch Data from Localhost Server Database using XAMPP

  • 这里我创建了名为 StudentInfo 的表。

  • 点击GO按钮执行查询并创建表。

  • 以下是创建表的脚本。

创建表的脚本:

雷雷

第3步:将数据插入表中

  • 从左侧边栏中选择新创建的数据库。

  • 单击“SQL”选项卡。

  • 输入以下 SQL 查询将数据插入表中:

PHP program to Fetch Data from Localhost Server Database using XAMPP

将数据插入表的脚本

雷雷

创建 PHP 文件:下一步我们需要创建 PHP 文件来从本地主机服务器数据库获取数据并显示记录,步骤如下。

第一步:打开任意一个IDE来编写PHP代码。这里我使用IntelliJ IDEA来编写PHP代码。

  • 启动 IntelliJ IDEA IDE 并创建一个要在其中创建程序的文件夹。

  • 现在转到文件 ->新->文件。

  • 给出以.PHP为扩展名的文件名,这里我创建的文件名为Fetch.php。

  • 这里我在新创建的PHP文件中输入了以下代码。

  • PHP program to Fetch Data from Localhost Server Database using XAMPP

  • Modify the database connection settings ($servername, $username, $password) according to your XAMPP configuration.

  • Change the database name and the table name from where you need to fetch the data.

  • Here I have created the database with the name “assignments”.

  • Make sure you add the correct server details

Script for the PHP program

<?php
// Database connection settings
$servername = "localhost";
$username = "root";
$password = "";
$database = "assignments";

// Create connection
$conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->connect_error) {
   die("Connection failed: " . $conn->connect_error);
}

// SQL query to fetch data from the table
$sql = "SELECT * FROM Studentinfo";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
   // Output data of each row
   while ($row = $result->fetch_assoc()) {
      echo "ID: " . $row["id"] . " - Name: " . $row["name"] . " - Email: " . $row["email"] ." - FatherName: " . $row["fathername"] ." - MobileNo: " . $row["mobileno"] . "<br>";
   }
} else {
   echo "No results found";
}

// Close the database connection
$conn->close();
?>
登录后复制

Save the file and then, access the PHP file through your web browser (e.g., http://localhost/fetch.php) to see the fetched data from the database displayed on the page.

Output

ID: 1 - Name: Kishore - Email: kish@gmail.com - FatherName: Ranga - MobileNo: 9347342900
ID: 2 - Name: Haveesh - Email: havi@gmail.com - FatherName: Kishore - MobileNo: 8341748800
ID: 3 - Name: Hasvitha - Email: hasvi@gmail.com - FatherName: Kishore - MobileNo: 8466906072
ID: 4 - Name: Santh - Email: San@gmail.com - FatherName: Suresh - MobileNo: 8466906072
登录后复制

Conclusion

To fetch data from a localhost server database using XAMPP, you can create a PHP program. Start by installing XAMPP and launching the Apache and MySQL services. Access phpMyAdmin to create a database and table. Then, create a PHP file in the appropriate directory and establish a connection to the MySQL database using the provided credentials. Execute an SQL query to fetch the desired data from the table and iterate through the results to display them. Finally, close the database connection. By accessing the PHP file through a web browser, you can see the fetched data displayed on the page. This process enables you to interact with the local server database using PHP and XAMPP, facilitating efficient data retrieval and utilization.

以上是使用 XAMPP 从本地主机服务器数据库获取数据的 PHP 程序的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
php
来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!