Python equivalent of PHP mysql_fetch_array

mysql, mysql-python, python, sql

Solution

- Install MySQLdb (the drivers for MySQL for Python). Type `pip install mysql-python`

- Read up on the Python DB API, which is the standard way to access databases in Python.

Then, try this:

>>> import MySQLdb
>>> connection = MySQLdb.connect(database='test')
>>> cursor = connection.cursor()
>>> cursor.execute('SELECT * FROM users WHERE firstname = %s',('somename',))
>>> results = cursor.fetchall()
>>> for i in results:
       print i

Problem

I would like to fetch an array in MySQL. Can someone please tell me how to use Python using MySQLdb to do so? For example, this is what I would like to do in Python: ``` <?php require_once('Config.php'); $q = mysql_query("SELECT * FROM users WHERE firstname = 'namehere'"); $data = mysql_fetch_array($q); echo $data['lastname']; ?> ``` Thanks.

Original source