PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, August 15, 2022

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

 August 15, 2022     database, flask, mysql, output     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing