繁体   English   中英

打印语句在PSP中不起作用(Python服务器页面)

[英]Print statement not working in PSP (Python server pages)

以下是python服务器页面(PSP)的代码; 使用mysqldb,我试图从表“ addr”中获取记录,然后将其打印到html页面上。

但是由于某种原因,在for循环内(包含表行)和在for循环外(包含“ Hello”语句)的print语句不起作用,这是因为在代码执行后,最后仅显示“ Rows printing”语句没别的。

<% import MySQLdb 

conn = MySQLdb.Connect(host='localhost',user='user',passwd='password',db='db1')
cur = conn.cursor()
sql= "SELECT * from addr;"
try:
  cur.execute(sql)
  data = cur.fetchall()
  for row in data:
    print row[1]
  print("Hello")
  cur.close()
  conn.close()
%>
<br />
<html>
<h1> Rows Printed</h1>
</body>
</html>
<%
except MySQLdb.IntegrityError,message:
  errorcode = message[0]
%>
<html>
<body><h1> Technical issue</h1>
</body>
</html>

即使没有try catch块,print语句也不会显示结果,但是作为Python脚本运行时,print语句可以正常工作,并提供存储在表中的所需记录。

我一生无法理解为什么要使用Python Server Pages,但是既然如此,显然您就不能使用print 您实际上需要使用PSP语法将结果输出到页面。 就像是:

for row in data: %>
<br><%= row[1] =%>
<%
cur.close()

等等

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM