gpt4 book ai didi

python - 错误消息 : End of script output before header while using MYSQL

转载 作者:行者123 更新时间:2023-11-30 22:24:07 25 4
gpt4 key购买 nike

我正在尝试在 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 没有。

最佳答案

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

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

import cgitb
cgitb.enable()

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

您的脚本还有其他问题。在 CGI 脚本中,标准输出作为 HTTP 回复发送。重要的是,必须先写入 HTTP header ,然后再写入实际内容。

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

关于python - 错误消息 : End of script output before header while using MYSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35810594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com