简体   繁体   中英

Print statement not working in PSP (Python server pages)

Below is a code for python server pages(PSP); using mysqldb, I am trying to fetch records from the table "addr" and then print the same onto an html page.

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.

<% 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.

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 . You need to actually output the results to the page in PSP syntax. Something like:

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

etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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