我正在尝试升级我的方式从mysql从mysqli_query获取到fetchall。
$res = mysqli_query($db, "SELECT * FROM forum_index WHERE forum_over='yes'");
while ($arr = mysqli_fetch_assoc($res)) {
......
}
所以当我使用fetchAll()时,我会得到一个数组,我应该使用foreach(),还是有更智能的方法呢?
要从数据库中收集单个值,这是正确的方法吗?
$fid = (int)$_GET['id'];
$thread = $db->query("SELECT * FROM forum_threads WHERE f_id=".$fid)->fetch_array();
echo $thread['id'];
答案 0 :(得分:2)
您不需要仅仅因为您正在使用PDO而使用import os, sys
from flask import Flask, render_template, flash, request, url_for, redirect, session, send_file, send_from_directory
# WTForms
from wtforms import Form, BooleanField, TextField, PasswordField, SelectField, RadioField, TextAreaField, DateField, DateTimeField, StringField, validators
from wtforms.widgets import TextArea
from wtforms.validators import DataRequired
from flask_wtf import FlaskForm, RecaptchaField
from flask_wtf.file import FileField, FileRequired, FileAllowed
from werkzeug.utils import secure_filename
from flask_uploads import UploadSet, configure_uploads, IMAGES, patch_request_class
from MySQLdb import escape_string as thwart
app = Flask(__name__)
#### PROFILE PIC UPLOAD ####
# Based after https://gist.github.com/greyli/81d7e5ae6c9baf7f6cdfbf64e8a7c037
# For uploading files
PROF_PIC_UPLOAD_FOLDER = 'static/user_info/prof_pic'
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
app.config['UPLOADED_PHOTOS_DEST'] = os.getcwd()
photos = UploadSet('photos', IMAGES)
configure_uploads(app, photos)
patch_request_class(app) # set maximum file size, default is 16MB
class ProfilePictureForm(FlaskForm):
prof_pic = FileField(validators=[FileAllowed(photos, u'Image only!')])
@app.route('/profile_picture_upload/', methods=['GET','POST'])
def profile_picture_upload():
form = ProfilePictureForm()
cid = str(session['clientcid'])
first_name = session['first_name']
static_url = url_for('static', filename='user_info/prof_pic/')
default_prof_pic = 'http://localhost:5000/_uploads/photos/static/user_info/prof_pic/default.jpg'
user_prof_pic = cid+first_name+'.png'
if form.validate_on_submit():
filename = photos.save(form.prof_pic.data, folder=PROF_PIC_UPLOAD_FOLDER,name=cid+first_name+'.png')
file_url = photos.url(filename)
# Checks if the prof_pic is set yet. if set, then dont need to delete the old picture on the server
if session['prof_pic'] != 'http://localhost:5000/_uploads/photos/static/user_info/prof_pic/default.jpg':
#need to delete or move the old prof_pic if it was set! Prevents users from adding too many pictures
flash("You already have a file on the server!")
#If the user_prof_pic is there, then
session['prof_pic'] = file_url
c, conn = connection()
c.execute("UPDATE cpersonals SET prof_pic = %s WHERE cid = (%s)", (file_url, cid))
conn.commit()
c.close()
conn.close()
else:
file_url = None
return render_template('profile_picture_upload.html', form=form, file_url=file_url)
。如果查询返回大量数据,这可能会减慢速度,因为它必须将所有数据全部收集到内存中。您可以使用与mysqli代码中相同类型的循环:
fetchAll()
关于第二个问题,你应该使用参数化查询,而不是替换变量。
$res = $pdo->query("SELECT * FROM forum_index WHERE forum_over='yes'");
while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
...
}