Monday, August 15, 2022

[FIXED] Why I get the wrong output format of counting with flask?

Issue

I'm now trying to count the number of data from the phpMyAdmin database. Why I will get the output like (2,) but is not only number format such as 2?

app.py code

@app.route("/adminPanel", methods=['GET', 'POST'])
def adminPanel():
    if 'adminName' in session:
    cur = mysql.connection.cursor()
    result1 = cur.execute("SELECT COUNT(*) FROM adminaccount")
    display2 = cur.fetchone()
    cur.close()

    adminName = session['adminName']
    return render_template("adminPanel.html", adminName=adminName, result1=result1, display2=display2)

else:
    return redirect(url_for('adminLogin'))

result result of the output


Solution

Select returns results, each result is a tuple of values. Your select asks for just one value, therefore you will get a tuple with just one value. So:

display2     # this is the whole row
display2[0]  # this is the value of the first "column"

Thus you have the same interface when dealing with 1 column per row and more columns per row.



Answered By - Petr Blahos
Answer Checked By - David Goodson (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.