title: 'NoneType' object has no attribute 'cursor'
body: <p>I have created a very simple web app using Flask and I have a MySQL database connected. FYI, I am using bash on windows.</p>
  
  <p>The below function registers a person into the Mysql Database and it works as expected. The cursor object is defined and data are saved into MySQL.</p>
  
  <pre><code>@app.route('/register', methods=['GET','POST'])
  def register():
      form = RegisterForm(request.form)
      if request.method == 'POST' and form.validate():
          name = form.name.data
          email = form.email.data
          username = form.username.data
          password = hash.encrypt(str(form.password.data))
  
          # Create cursor
          cur = mysql.connection.cursor()
          cur.execute("INSERT INTO users(name, email, username, password) VALUES(%s, %s, %s, %s)",
          [name, email, username, password])
  
          # commit to db
          mysql.connection.commit()
  
          # close connection
          cur.close()
  
          flash('You are now registered and can log in', 'success')
  
          return redirect(url_for('login'))
  
      return render_template('register.html', form=form)
  </code></pre>
  
  <p>The problem starts when I want to load data from mysql:</p>
  
  <pre><code>def data():
      cur = mysql.connection.cursor()
      cur.execute("SELECT * FROM users")
      mysql.connection.commit()
      cur.close()
  
  data()
  </code></pre>
  
  <p>I get the error: </p>
  
  <blockquote>
    <p>File "app.py", line 23, in data
        cur = mysql.connection.cursor()
    AttributeError: 'NoneType' object has no attribute 'cursor'</p>
  </blockquote>
  
  <p>As pointed out by @Martijn Pieters, this means that I could not connect to the mysql database. The question is, why does flask connect without a problem in the first function and has issues with the second function? </p>
  
  <p>Below are my imports for replication:</p>
  
  <pre><code>from flask import Flask, render_template, flash, request, redirect, url_for, session, logging, url_for
  from data import Articles
  from flask_mysqldb import MySQL
  from wtforms import Form, StringField, TextAreaField, PasswordField, validators
  from passlib.hash import sha256_crypt
  
  app = Flask(__name__)
  
  #init MYSQL
  mysql=MySQL(app)
  </code></pre>