gpt4 book ai didi

Python 等效于 PHP mysql_fetch_array

转载 作者:可可西里 更新时间:2023-11-01 06:57:48 25 4
gpt4 key购买 nike

我想在 MySQL 中获取一个数组。有人可以告诉我如何使用 Python 和 MySQLdb 来做到这一点吗?

例如,这是我想用 Python 做的:

<?php

require_once('Config.php');

$q = mysql_query("SELECT * FROM users WHERE firstname = 'namehere'");
$data = mysql_fetch_array($q);
echo $data['lastname'];

?>

谢谢。

最佳答案

在 python 中你有 dictionary=True,我已经在 python3 中测试过。这将返回与 php 中的关联数组非常相似的目录。例如。

import mysql.connector
cnx = mysql.connector.connect(user='root', password='',host='127.0.0.1',database='test1')
cursor = cnx.cursor(dictionary=True)
sql= ("SELECT * FROM `users` WHERE id>0")
cursor.execute(sql)
results = cursor.fetchall()
print(results)

关于Python 等效于 PHP mysql_fetch_array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11565487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com