用python+php写一个读取数据库系统
时间: 2023-08-04 09:24:11 浏览: 52
要实现读取数据库系统,通常需要使用Python和PHP的数据库扩展模块,以及数据库的连接信息。以下是一个使用Python和PHP读取MySQL数据库的示例代码:
Python代码:
```python
import pymysql
# 连接数据库
conn = pymysql.connect(host='localhost', user='root', password='123456', database='test')
cursor = conn.cursor()
# 查询数据
sql = "SELECT * FROM user"
cursor.execute(sql)
result = cursor.fetchall()
print(result)
# 关闭数据库连接
cursor.close()
conn.close()
```
PHP代码:
```php
<?php
// 连接数据库
$conn = mysqli_connect("localhost", "root", "123456", "test");
// 查询数据
$sql = "SELECT * FROM user";
$result = mysqli_query($conn, $sql);
// 输出数据
while ($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"] . " - name: " . $row["name"] . " - age: " . $row["age"] . "<br>";
}
// 关闭数据库连接
mysqli_close($conn);
?>
```
以上代码是一个简单的读取MySQL数据库的示例,你可以根据自己的需求进行相应的修改和扩展。需要注意的是,如果涉及到数据库的写操作,一定要注意数据安全性,避免被恶意攻击。