MYSql, CGI web page search code not working

Kirk McDonald mooquack at suad.org
Sat Jan 28 12:58:51 EST 2006


Fred wrote:
> No matter what I type in the form text box (or even if I leave it
> blank) I get all the records.

Try this:

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

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

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>'

(Assuming the name of your text field is "name".)



More information about the Python-list mailing list