I am trying to print out some database resutls as follows

cursor.execute("SELECT * FROM User")
        numrows_user = int(cursor.rowcount)
        i_user = int(0)
        Results_user = cursor.fetchall()


        
        while i_user < numrows_user:
           print Results_user[i_user]["ID"]

Put i am getting nothing i know i_user is working and i know there is information in the database if i do something like

print Results_user[0]["ID"]

i get info printed out. Why wont it work with the loop

what happens when you run the code? it "should" hit the while and continuously print (you have an infinite loop there). if you are not getting into a infinite loop, numrows_user is probably not set to what you expect.

its not an infinite loop as numrows_user is part of a database search earlier on and when i print it out it = 4.

Whats supposed to happen when i run the code is the 4 ID from a data base table is printed out. This is the whole code i am trying to run

#drop down boxes
        #execute SQL select statement
        cursor.execute("SELECT * FROM User")
        numrows_user = int(cursor.rowcount)
        i_user = 0
        Results_user = cursor.fetchall()


        
        while i_user < numrows_user:
            for element in Results_user[i_user].keys():
                if element == "UserName":
                    if Results_user[i_user][element] != Results[i][subelement]:
                        print "<option value=\""+Results_user[i_user][element]+"\">"+Results_user[i_user][element]+"</option>"
                        i_user = i_user + 1
                    else:
                        i_user = i_user + 1
                        continue
                else:
                    continue
        print "</select>"
        print "<br />"

The above code actually works but i need to change Results_user[i_user][element] to Results_user[i_user]["ID"] and thats when i get nothing

when i try and print out Results_user[i_user][element] it works but when i specify element i get nothing. But if i do Results_user[0]["ID"] i get the one result printed out.

ok i have re writtent the code and its now working but the code is sloppy and there must be a better way of doing this

#!/usr/bin/python
import MySQLdb
import os
import Cookie
import time 
print "Content-type: text/html\n"
# connect
db = MySQLdb.connect(host = "localhost", user = "dfgfdgfdgdfgdfg", passwd = "fdgdfgfd", db = "afgfdgdfgfdgdfg")

# create a database cursor
cursor = db.cursor(MySQLdb.cursors.DictCursor)

#execute SQL select statement
cursor.execute("SELECT * FROM on_call")
numrows = int(cursor.rowcount)
Results = cursor.fetchall()

#getting all users name
cursor.execute("SELECT UserName, ID FROM User")
numrows = int(cursor.rowcount)
Result = cursor.fetchall()
row = cursor.rowcount
i = 0



print "<form action=\"update_contact_schedule.py\" method=\"post\">"

for subelement in Results[0].keys():
    i = 0
    if subelement == "Monday":
        print "Monday:"
        
        #print whos currently on call for the day
        cursor.execute("SELECT UserName FROM User WHERE ID =" + Results[0][subelement])
        User = cursor.fetchall()
        print "<select name=\"Monday\">"
        print "<option value=\""+Results[0][subelement]+"\">"+User[0]["UserName"]+"</option>"
        while i < row:
            if Result[i]["UserName"] == User[0]["UserName"]:
                i = i + 1
            else:
               
                print "<option value=\""
                print Result[i]["ID"]
                print "\">"
                print Result[i]["UserName"]
                print "</option>"
                i = i + 1
        print "</select>"    
        print "<br />"
        

    elif subelement == "Tuesday":
        print "Tuesday:"
        
        #print whos currently on call for the day
        cursor.execute("SELECT UserName FROM User WHERE ID =" + Results[0][subelement])
        User = cursor.fetchall()
        print "<select name=\"Tuesday\">"
        print "<option value=\""+Results[0][subelement]+"\">"+User[0]["UserName"]+"</option>"
        while i < row:
            if Result[i]["UserName"] == User[0]["UserName"]:
                i = i + 1
            else:
               
                print "<option value=\""
                print Result[i]["ID"]
                print "\">"
                print Result[i]["UserName"]
                print "</option>"
                print "<br />"
                i = i + 1
        print "</select>"    
        print "<br />"

    elif subelement == "Wednesday":
        print "Wednesday:"
        
        #print whos currently on call for the day
        cursor.execute("SELECT UserName FROM User WHERE ID =" + Results[0][subelement])
        User = cursor.fetchall()
        print "<select name=\"Wednesday\">"
        print "<option value=\""+Results[0][subelement]+"\">"+User[0]["UserName"]+"</option>"
        while i < row:
            if Result[i]["UserName"] == User[0]["UserName"]:
                i = i + 1
            else:
               
                print "<option value=\""
                print Result[i]["ID"]
                print "\">"
                print Result[i]["UserName"]
                print "</option>"
                print "<br />"
                i = i + 1
        print "</select>"    
        print "<br />"
        
    elif subelement == "Thursday":
        print "Tursday:"
        
        #print whos currently on call for the day
        cursor.execute("SELECT UserName FROM User WHERE ID =" + Results[0][subelement])
        User = cursor.fetchall()
        print "<select name=\"Thursday\">"
        print "<option value=\""+Results[0][subelement]+"\">"+User[0]["UserName"]+"</option>"
        while i < row:
            if Result[i]["UserName"] == User[0]["UserName"]:
                i = i + 1
            else:
               
                print "<option value=\""
                print Result[i]["ID"]
                print "\">"
                print Result[i]["UserName"]
                print "</option>"
                print "<br />"
                i = i + 1
        print "</select>"    
        print "<br />"

    elif subelement == "Friday":
        print "Friday:"
        
        #print whos currently on call for the day
        cursor.execute("SELECT UserName FROM User WHERE ID =" + Results[0][subelement])
        User = cursor.fetchall()
        print "<select name=\"Friday\">"
        print "<option value=\""+Results[0][subelement]+"\">"+User[0]["UserName"]+"</option>"
        while i < row:
            if Result[i]["UserName"] == User[0]["UserName"]:
                i = i + 1
            else:
               
                print "<option value=\""
                print Result[i]["ID"]
                print "\">"
                print Result[i]["UserName"]
                print "</option>"
                print "<br />"
                i = i + 1
        print "</select>"    
        print "<br />"

    elif subelement == "Saturday":
        print "Saturday:"
        
        #print whos currently on call for the day
        cursor.execute("SELECT UserName FROM User WHERE ID =" + Results[0][subelement])
        User = cursor.fetchall()
        print "<select name=\"Saturday\">"
        print "<option value=\""+Results[0][subelement]+"\">"+User[0]["UserName"]+"</option>"
        while i < row:
            if Result[i]["UserName"] == User[0]["UserName"]:
                i = i + 1
            else:
               
                print "<option value=\""
                print Result[i]["ID"]
                print "\">"
                print Result[i]["UserName"]
                print "</option>"
                print "<br />"
                i = i + 1
        print "</select>"    
        print "<br />"


    elif subelement == "Sunday":
        print "Sunday:"
        
        #print whos currently on call for the day
        cursor.execute("SELECT UserName FROM User WHERE ID =" + Results[0][subelement])
        User = cursor.fetchall()
        print "<select name=\"Sunday\">"
        print "<option value=\""+Results[0][subelement]+"\">"+User[0]["UserName"]+"</option>"
        while i < row:
            if Result[i]["UserName"] == User[0]["UserName"]:
                i = i + 1
            else:
               
                print "<option value=\""
                print Result[i]["ID"]
                print "\">"
                print Result[i]["UserName"]
                print "</option>"
                print "<br />"
                i = i + 1
        print "</select>"    
        print "<br />"


print "<br />"
print "<br />"
print "<input type=\"submit\" value=\"Submit\">"
print "</form>"

any ideas?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.