错误消息:使用MYSQL时头文件输出结束

时间:2016-03-05 05:46:47

标签: python mysql cgi-bin

我正在尝试在python cgi-bin中实现MYSQL。我不知道为什么会这样。 我在Windows 8和python版本2.7.8上执行此操作。我也参观了不同但未能解决的问题。

文件名: sqlcgi.py

我的代码:

#!C:\Python27\python
import cgi
import MySQLdb

db = MySQLdb.connect(host="127.0.0.1",port=3306, user="root", passwd="",db="project")

cursor = db.cursor() #for 1st query
cursor2 = db.cursor()##for 2nd query
# execute SQL select statement
#sql = "SELECT stid,firstname FROM student WERE stid IN(SELECT stid FROM stobt WHERE stid=%s" % (0) #query number 1

#query number 1
sql2 = "SELECT stid FROM student  WHERE firstname = '%s'" % ('User')


# Execute the SQL command

cursor2.execute(sql2)
# Fetch all the rows in a list of lists.

result2 = cursor2.fetchall()
print result2

row2 = result2[0]
#print 'r1',row[1]
#print 'r2',row2[0]
sql = "SELECT * FROM stobt  WHERE stid = '%d'" % (row2)
cursor.execute(sql)
results = cursor.fetchall()
row = results[1]
tc = 56
for row in results:

   if row2[0] == row[1]:
      idd = row[0]
      stid = row[1]
      obtained = row[2]
      subject = row[3]

   # Now print fetched result
      print "id=%s,stid=%s,obtained=%f,subject=%s" % \
             (idd, stid, obtained, subject )

      if obtained <= 50:
         if tc >= 50:
            print tc
print """Content-type: text/html\n

<html><head><title>MySQL and Python on the Web</title></head>
<body bgcolor=#FFCCFF><h1>Message Board Report</h1>
This is a message board report demo.<hr>
<b>This is what has been going on ... </b><br>
"""

print idd

print """
<hr>
<form method=POST>Please enter your comment: <input name=info><br>
<input type=submit>
</form>
<hr>
Demonstration from Well House Consultants.<br>
<a href=http://www.wellho.net/>Website</a>
</body></html>
"""

# disconnect from server
db.close()

错误:

Error message: 
End of script output before headers: sqlcgi.py

我不知道我做错了还是正确的,因为当我删除所有sql部分和cgi打印但是sql没有。

1 个答案:

答案 0 :(得分:0)

在第一个输出语句之前某处出现错误,并且您的脚本正在中止。

如果启用追溯报告,将有助于找到它。将其添加到脚本的顶部:

import cgitb
cgitb.enable()

在浏览器中加载页面时,您应该看到错误是什么。

您的脚本还存在其他问题。在CGI脚本中,标准输出作为HTTP回复发送。重要的是,必须首先在实际内容之前写入HTTP标头。

在这种情况下,您有一些调试打印语句,例如第22行,在打印HTTP标头之前生成输出。

相关问题