Last active
August 29, 2015 13:58
-
-
Save davepape/9936761 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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