Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python MySqldb和CGI不在一起工作_Python_Mysql_Python 2.7_Cgi - Fatal编程技术网

Python MySqldb和CGI不在一起工作

Python MySqldb和CGI不在一起工作,python,mysql,python-2.7,cgi,Python,Mysql,Python 2.7,Cgi,我正在windows上使用2.7.8。我对发生这种情况的原因感到紧张,并进行了大量搜索。就像我将cgi代码放在第一位并检查它是否正常工作一样,但是当我只是放置import MySQLdb时,它不会执行,页面显示服务器错误 以下是未成功打印的代码import MySqldb: 文件名:cgisql.py #!C:\Python27\python # Import modules for CGI handling import cgi # Create instance of FieldSt

我正在windows上使用2.7.8。我对发生这种情况的原因感到紧张,并进行了大量搜索。就像我将cgi代码放在第一位并检查它是否正常工作一样,但是当我只是放置
import MySQLdb
时,它不会执行,页面显示
服务器错误

以下是未成功打印的代码
import MySqldb

文件名:
cgisql.py

#!C:\Python27\python
# Import modules for CGI handling 

import cgi


# Create instance of FieldStorage 
form = cgi.FieldStorage() 

# Get data from fields
first_name = form.getvalue('first_name')
last_name  = form.getvalue('last_name')

print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Hello - Second CGI Program</title>"
print "</head>"
print "<body>"
print "<h2>Hello %s %s</h2>" % (first_name, last_name)
print "</body>"
print "</html>"
#!C:\Python27\python
#导入用于CGI处理的模块
导入cgi
#创建FieldStorage的实例
form=cgi.FieldStorage()
#从字段中获取数据
first\u name=form.getvalue('first\u name')
last\u name=form.getvalue('last\u name')
打印“内容类型:text/html\r\n\r\n”
打印“”
打印“”
打印“Hello-第二个CGI程序”
打印“”
打印“”
打印“Hello%s%s”%(姓、名)
打印“”
打印“”

使用导入MySqldb

#!C:\Python27\python
# Import modules for CGI handling 

import cgi
import MySQLdb

# Create instance of FieldStorage 
form = cgi.FieldStorage() 

# Get data from fields
first_name = form.getvalue('first_name')
last_name  = form.getvalue('last_name')

print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Hello - Second CGI Program</title>"
print "</head>"
print "<body>"
print "<h2>Hello %s %s</h2>" % (first_name, last_name)
print "</body>"
print "</html>"
#!C:\Python27\python
#导入用于CGI处理的模块
导入cgi
导入MySQLdb
#创建FieldStorage的实例
form=cgi.FieldStorage()
#从字段中获取数据
first\u name=form.getvalue('first\u name')
last\u name=form.getvalue('last\u name')
打印“内容类型:text/html\r\n\r\n”
打印“”
打印“”
打印“Hello-第二个CGI程序”
打印“”
打印“”
打印“Hello%s%s”%(姓、名)
打印“”
打印“”
第页上的输出:


对不起,我现在面临一些大问题,我真的不知道这个谜团是什么

我终于找到了解决方案: 只需添加:

import cgitb
cgitb.enable()
检查错误

出现错误:

因此,最终解决方案: 加

这将100%解决问题。。。。。
另外,多亏了Dreamer

您应该先检查日志。您是否确实安装了
MySQLdb
?还介绍了如何启用错误,以便您可以看到出现了什么问题。如果30分钟内没有人回复,请不要留下评论。它不会让人们回复得更快,只会让你变得专横和咄咄逼人。我在控制台上写道:导入MySQLdb,它没有显示任何错误这就是我得到的:
import os
os.environ['PYTHON_EGG_CACHE'] = '/tmp'