Reputation: 13
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.
Upvotes: 0
Views: 548
Reputation: 599580
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.
Upvotes: 0