简体   繁体   English

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

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

Below is a code for python server pages(PSP); 以下是python服务器页面(PSP)的代码; using mysqldb, I am trying to fetch records from the table "addr" and then print the same onto an html page. 使用mysqldb,我试图从表“ addr”中获取记录,然后将其打印到html页面上。

But for some reason the print statement inside the for loop (containing table rows) and outside the for loop(containing "Hello" statement) isn't working due to which after the code execution at last only the statement "Rows printed" is displayed and nothing else. 但是由于某种原因,在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>

The print statement doesnt display the results even without the try catch block, but the same when run as a Python script works fine and provides the desired records stored in the table. 即使没有try catch块,print语句也不会显示结果,但是作为Python脚本运行时,print语句可以正常工作,并提供存储在表中的所需记录。

I can't for the life of me understand why you'd want to use Python Server Pages, but since you are, you obviously can't use print . 我一生无法理解为什么要使用Python Server Pages,但是既然如此,显然您就不能使用print You need to actually output the results to the page in PSP syntax. 您实际上需要使用PSP语法将结果输出到页面。 Something like: 就像是:

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

etc. 等等

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

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