Skip to content

Instantly share code, notes, and snippets.

@davepape
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davepape/9936761 to your computer and use it in GitHub Desktop.
Save davepape/9936761 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# Query the 'world' database for a country whose name (or partial name) was entered via queryform.html
import MySQLdb
db = MySQLdb.connect(host='localhost', port=3306, user='myusername', passwd='mypassword', db='world')
cur = db.cursor()
cur.execute("set names 'utf8'")
import cgi
fields = cgi.FieldStorage()
countryname = fields['name'].value
print '''Content-type: text/html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>web page</title>
</head>
<body>
'''
print "<p>You want info on %s</p>" % countryname
print "<ul>"
sql = "select Name, Continent, Population, Code from Country where Name like '%" + db.escape_string(countryname) + "%'"
cur.execute(sql)
for row in cur.fetchall():
print "<li>", row[0], "is in", row[1], "and has population", row[2]
print "</ul>"
print '''
</body>
</html>
'''
cur.close()
db.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment