While most users will not have a need to use Python to connect to their MySQL database, there have been a few times when we've had questions about how to do that with Python.
At this point in the process you should already created a MySQL database and have a user assigned to that database.
You will need the following settings for your script. Please note that the settings we are including below are for our testing purposes, please replace these with your settings (database host should stay the same).
database name: whhsup5_python
database user: whhsup5_python
database password: pytest
database host: localhost
Once we have the database setup, we can create our script and save it to the cgi-bin folder as pythontest.py (name your file however you like, just remember the .py extension). Once the file has been created, change the fle permissions to 755 (as all files in the cgi-bin should be).
This script will connect to and run the SELECT VERSION(), command which will show us what version of MySQL our database is running.
#!/usr/bin/env python
import MySQLdb
# connect to the database
db = MySQLdb.connect("localhost","whhsup5_python","pytest","whhsup5_python" )
# setup a cursor object using cursor() method
cursor = db.cursor()
# run a sql question
cursor.execute("SELECT VERSION()")
# grab one result
data = cursor.fetchone()
# begin printing data to the screen
print "Content-Type: text/html"
print
print """\
<html>
<head>
<title>Python - Hello World</title>
</head>
<body>
"""
print "Database version : %s " % data
print"""\
</body>
</html>
"""
# close the mysql database connection
db.close()
Now, let's bring up pythontest.py in a browser and we should see:
Congratulations, you've just used Python to connect to your MySQL database!
n/a Points
|
2015-05-18 2:43 am
This was very helpful but I'm still having issues. After following the directions above I am getting the following error. Any ideas? Internal Server ErrorThe server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator, webmaster@bitcoax.flintknap.com and inform them of the time the error occurred, and anything you might have done that may have caused the error. More information about this error may be available in the server error log. Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request. |
Staff 16,266 Points
|
2015-05-19 12:04 am
Hello Jason,
A 500 error can be caused by a number of things. You may want to check out our article on 500 errors to see if that helps you. Kindest Regards, Scott M |
Email: | support@WebHostingHub.com | Ticket: | Submit a Support Ticket |
---|---|---|---|
Call: | 757-416-6627 | Chat: | Click To Chat Now |
We value your feedback!
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.
new! - Enter your name and email address above and we will post your feedback in the comments on this page!