簡體   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