MYSql, CGI web page search code not working

Kirk McDonald mooquack at suad.org
Sat Jan 28 13:14:44 EST 2006


Fred wrote:
> Yeah, I already tried that (except you have a , after name.
> 
> Your code produces the same error:
> 
>  NameError: name 'name' is not defined
> 
> I know I am close!! Just missing some small thing...
> 

Oh, duh. I forgot something:

#!/usr/local/bin/python
print "Content-Type: text/html\n"
import MySQLdb
import cgi

form = cgi.FieldStorage()

db=MySQLdb.connect(host = 'localhost', db = 'phone')
cursor=db.cursor()
cursor.execute("Select * from phone where name=%s order by name",
     (form['name'].value,))

result = cursor.fetchall()
for record in result:
     print '<p>'
     print record[0]
     print '--'
     print record[1]
     print '--'
     print record[2]
     print '--'
     print record[3]
     print '</p>'

The comma is intentional: the MySQLdb wants the argument(s) as a tuple.



More information about the Python-list mailing list