我希望按用户ID获取信息,因此我们将其添加到模型中:
public function getById ($id)
{
$sql = 'SELECT * FROM users';
return ActualDbHander::run($sql);
}
之后,我想只获得一些字段:
public function getById ($id, $fields = '*')
{
$sql = 'SELECT '.$fields.' FROM users';
return ActualDbHander::run($sql);
}
另一个想法,让我们添加排序:
public function getById ($id, $fields = '*', $orderBy = '')
{
$sql = 'SELECT '.$fields.' FROM users';
if ($orderBy != '')
{
$sql.= ' ORDER BY '.$orderBy;
}
return ActualDbHander::run($sql);
}
我觉得这很麻烦而且凌乱。如果我想添加JOIN-s怎么办?如果我想添加详细的WHERE-s怎么办?这就是“太普通”的方法诞生的时候。
答案 0 :(得分:1)
我完全同意mch和Mjh的评论,但是,只有在你真的想拥有一个“BD驱动程序”(并自己构建)的情况下,我会为每个查询使用不同的名称,非常具体的名称,因为您需要准确了解函数将返回给您的内容。
所以,如果我是你,我会使用getAllUsers
,getUserById
,getAllUsersOnlyPersonalData
,getUserByIdOnlyPersonalData
,getAllUsersOnlyContactData
之类的名称等等(与固定字段和每个方法的过滤器)。
请注意,在您的示例中,您并未使用所有$ id变量,因此您始终会收到一个用户列表。
关于进行查询的方法,有很多方法可以做到。就个人而言,我更喜欢 MySQLi面向对象的预处理语句,因为它安全,简单且目前非常扩展,因此我将仅使用它来说明示例。
您的功能将是这样的:
<?php
class DBDriver{
function openConnection(){
// If you don't always use same credentials, pass them by params
$servername = "localhost";
$username = "username";
$password = "password";
$database = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Return conection object
return $conn;
}
function closeConnection($conn){
$conn->close();
}
function getAllUsers (){ // We don't need ids here
$conn = $this->openConnection();
// Array of arrays to store the results
// You can use any other method you want to return them
$resultsArray = [];
$sqlQuery = "SELECT * FROM users";
// In this case it's not neccesary to use prepared statements because we aren't binding any param but we'll use it to unify the method
if ($stmt = $conn->prepare($sqlQuery)) {
// Execute query
$stmt->execute();
// Bind result variables (I don't know your actuall column names)
$stmt->bind_result($id, $name, $email, $phone, $birthdate);
// Fetch values
while ($stmt->fetch()) {
$resultsArray[] = [$id, $name, $email, $phone, $birthdate];
}
// Close statement
$stmt->close();
}
$this->closeConnection($conn);
// If no results, it returns an empty array
return $resultsArray;
}
function getUserByIdOnlyContactData ($userId){
$conn = $this->openConnection();
// Array to store the results (only one row in this case)
$resultsArray = [];
$sqlQuery = "SELECT name, email, phone FROM users WHERE id = ?";
if ($stmt = $conn->prepare($sqlQuery)) {
// Bind parameter $userId to "?" marker in $sqlQuery
$stmt->bind_param("i", $userId);
$stmt->execute();
$stmt->bind_result($name, $email, $phone);
// If id found
if ($stmt->fetch()) {
$resultsArray = [$name, $email, $phone];
}
// Close statement
$stmt->close();
}
$this->closeConnection($conn);
return $resultsArray;
}
function getAllUserOnlyBirthdayDataOrderByBirthday (){
$conn = $this->openConnection();
$resultsArray = [];
$sqlQuery = "SELECT id, name, birthdate FROM users ORDER BY birthdate";
if ($stmt = $conn->prepare($sqlQuery)) {
$stmt->execute();
$stmt->bind_result($id, $name, $birthdate);
while ($stmt->fetch()) {
$resultsArray[] = [$id, $name, $birthdate];
}
// Close statement
$stmt->close();
}
$this->closeConnection($conn);
return $resultsArray;
}
} // Class end
这种方式确实会根据您的要求提供很多功能,但是您可以看到添加新功能或修改它们非常容易(并且您不会对同一功能中的许多不同选项感到生气)。
希望这可以帮助您组织数据库驱动程序!