我正在使用 php 和 sql 制作一个大型网站,该网站将包含许多用户,并且我需要尽快建立我的数据库。 这是从名为 upload 的数据库表中获取 posts 值并将其存储在数组中的代码
//connect to database
$a = 'localhost';
$b = 'root';
$c = '';
$d = 'petsnote';
$connect = mysqli_connect($a, $b, $c, $d);
//get posts images variables
$images = array();
$cat = array();
$id = array();
//get posts info
if($connect){
$query = "SELECT * FROM `upload` WHERE `Active`='1'";
if($query_run = mysqli_query($connect, $query)){
while($get = mysqli_fetch_assoc($query_run)){
array_push($images, $get['Image_Name']);
array_push($cat, $get['PostCategory']);
array_push($id, $get['Id']);
}
$loop = count($images);
}
}
我从数据库中获取所有帖子信息并将它们存储在一个数组中,并使用该数组仅查看某些值。 这是一种高效、专业、快速的做事方式吗?
最佳答案
您的查询只需询问它将使用的内容:
SELECT Id, Image_Name, PostCategory FROM upload WHERE Active='1'
但这不会显着加快任何速度。
更重要的是数据库结构。从 MySQL 运行 SHOW CREATE TABLE upload
并确保您要搜索的每一列(例如本例中的 Active
)都已建立索引。
不过,除非您要处理数万行,否则您不会看到太大的差异。
关于php - Sql高效快速搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34297044/