给这个帖子评分
本文主要介绍使用Python代码与MySQL一起工作,并假定你已经完成了以下文章。
以及以下步骤。
- 启动EasyPHP
- 打开仪表板
- 启动HTTP和数据库服务器
过滤数据库并显示结果
在我们的文章CSV到MySQL表[EasyPHP + PHPMyAdmin]中,我们导入了一个CSV文件并重命名了数据库和表。
让我们继续前进,写一个Python脚本来连接到这个数据库表,过滤记录,并以HTML网页的形式输出。
安装所需的库
导航到一个IDE,打开一个终端会话。在终端提示符下,输入以下代码,并按下<Enter> 键,开始安装过程。
pip install mysql
如果成功,将显示一条信息表明这一点。
在EasyPHP-Devserver-17文件夹内创建一个文件夹
导航到前面文章中创建的快捷文件夹(位于桌面上)。双击以打开该文件夹。
创建一个新的文件夹,在 **eds-www**文件夹,名为 FINXTER.
创建Python脚本
在IDE中,创建一个新的Python文件,名为connect.py 。复制并粘贴下面的内容到这个文件。把这个文件保存到 **FINXTER**文件夹中。
import mysql.connector
from mysql.connector import Error
config = {
'user': 'root',
'password': '',
'host': 'localhost',
'database': 'finxters_db',
'raise_on_warnings': True
}
try:
con = mysql.connector.connect(**config)
except Error as e:
print(f"The error '{e}' occurred")
mycursor = con.cursor()
mycursor.execute(f'USE finxters_db;')
con.commit()
mycursor.execute("SELECT First_Name, Last_Name, Username, Rank, Solved FROM users_tbl WHERE Rank='Authority';")
myresult = mycursor.fetchall()
print(f"<p align=center>{len(myresults)} records returned</p>")
print("<table class='table'>")
print("<thead>")
print(" <th>First Name</th>")
print(" <th>Last Name</th>")
print(" <th>Username</th>")
print(" <th>Rank</th>")
print(" <th>Solved</th>")
print("</thead>")
for r in myresults:
print("<tr>")
print(f"<td>{r[0]}</td>")
print(f"<td>{r[1]}</td>")
print(f"<td>{r[2]}</td>")
print(f"<td>{r[3]}</td>")
print(f"<td>{r[4]}</td>")
print("</tr>")
print("</table>")
print("<br/><br/>")
con.close()
这个脚本做了以下工作。
- 导入所需的
MySQL.connector库,以使Python能够与MySQL连接。
import mysql.connector
from mysql.connector import Error
config变量已经创建。这包含了所有相关的信息,以便连接到finxters_db数据库的所有相关信息。- 用户:
root。 - 主机:
localhost。 - 密码:
''(一个空字符串)。 - Raise warnings 选项。
- 用户:
**注意:**在一个安全的生产环境中,这些凭证将被锁定。
config = {
'user': 'root',
'password': '',
'host': 'localhost',
'database': 'finxters_db',
'raise_on_warnings': True
}
- 然后,Python试图在一个语句中连接到选定的数据库。
[try/except](https://blog.finxter.com/python-try-except-an-illustrated-guide/)语句中连接到选定的数据库。如果成功,代码继续执行。否则,脚本结束,并显示一个错误信息。
try:
con = mysql.connector.connect(**config)
except Error as e:
print(f"The error '{e}' occurred")
- 下面的部分创建了一个
[cursor()](https://dev.mysql.com/doc/refman/8.0/en/cursors.html)并使用execute()来传递MySQL 语句的**USE finxters_db**作为一个参数。这样做,是为了让MySQL 知道要引用什么数据库。
mycursor = con.cursor()
mycursor.execute(f'USE finxters_db;')
- 下面的部分从
users_tbl中调用11个列中的5个,并根据特定的Rank过滤结果。结果保存到myresults。
mycursor.execute("SELECT First_Name, Last_Name, Username, Rank, Solved FROM users_tbl WHERE Rank='Authority';")
myresult = mycursor.fetchall()
**注意:**你可能想导航到phpMyAdmin并熟悉以下内容
finxters_db 和 users_tbl.
- 最后一节以HTML表的形式显示结果总数和找到的匹配记录,并关闭数据库连接。
print(f"<p align=center>{len(myresults)} records returned</p>")
print("<table class='table'>")
print("<thead>")
print(" <th>First Name</th>")
print(" <th>Last Name</th>")
print(" <th>Username</th>")
print(" <th>Rank</th>")
print(" <th>Solved</th>")
print("</thead>")
for r in myresults:
print("<tr>")
print(f"<td>{r[0]}</td>")
print(f"<td>{r[1]}</td>")
print(f"<td>{r[2]}</td>")
print(f"<td>{r[3]}</td>")
print(f"<td>{r[4]}</td>")
print("</tr>")
print("</table>")
print("<br/><br/>")
con.close()
在我们创建一个PHP脚本并调用connect.py ,我们将无法看到这段代码的工作。
创建PHP脚本
我们必须创建一个PHP文件来使Python脚本在这个例子中工作。
导航到IDE,并创建PHP文件index.php 。复制并粘贴下面的内容到这个文件。将此文件保存到 **FINXTER**文件夹中。
<!DOCTYPE html>
<html>
<head>
<title>Finxter Users</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<style>
h1 {
color: goldenrod;
padding-top: 30px;
text-align: center;
text-shadow: 2px 2px ghostwhite;
}
h2 {
font-size: 19px;
text-align: center;
padding-bottom: 20px;
letter-spacing: 2px;
}
h3 {
font-size: 15px;
text-align: center;
padding-bottom: 30px;
letter-spacing: 1px;
}
p {
font-size: 14px;
}
table {
background-color: #f5f5f5;
}
th {
background-color: #F5F5F5;
color: black;
font-weight: 600;
}
tr {
line-height: 11px;
min-height: 11px;
height: 11px;
}
</style>
</head>
<body>
<div class="container">
<h1>Finxter Users</h1>
<h2>Authority Level</h2>
<?php
$output = shell_exec("python connect.py");
echo $output;
?>
</div>
<br/><br/>
</body>
</html>
正如你在上面的代码中所看到的,我们创建了一个HTML网页,并添加了一些CSS样式以获得时尚的输出。然而,让我们把注意力集中在上面用黄色标注的PHP行上。
你可以跳出HTML,通过添加以下代码来实例化PHP。
<?php |
在PHP的开头和结尾标签之间,添加了PHP代码来调用 shell_exec()函数。这个函数将python connect.py ,作为一个参数。
<?php |
接下来,connect.py 的内容被读入并保存到PHP变量$output 。只有在调用索引文件时,$output的内容才被打印出来。
<?php |
把它放在一起!
现在,connect.py 和index.php 两个文件都在上面概述的FINXTER 文件夹中;让我们在浏览器中查看它吧!
在EasyPHP仪表板上,点击下面的图标来展开。
点击打开FINXTER 文件夹。
点击index.php ,打开一个浏览器标签,查看结果。
如果成功的话,Python代码会与Python代码一起显示以下HTML页面。
要查看由connect.py 生成的HTML,在上述网页上用鼠标右键单击,显示一个弹出菜单。在这个弹出菜单中,选择查看页面源。向上和向下滚动以查看HTML和CSS代码。
另一种查看方法是在浏览器标签中输入 **localhost:8080**在浏览器标签页中,导航到 FINXTER 文件夹,然后点击选择index.php 。
芬克斯特的挑战修改
connect.py中的HTML代码。
重新加载/刷新网页以查看变化。祝您好运!
总结
在下面的文章中,在EasyPHP中安装Python应用程序,我们更新所需的应用程序,并安装Python应用程序以直接与EasyPHP工作。
在下面的文章中见!