用PHP编写CRUD方法的最佳方法
我已经尝试在PHP中为所有数据库活动更正CRUD方法,我将在下面发布我的代码用PHP编写CRUD方法的最佳方法,php,class,oop,Php,Class,Oop,我已经尝试在PHP中为所有数据库活动更正CRUD方法,我将在下面发布我的代码 class Database { //Connection Variable public $dbc; public function __construct() { self::dbconnect('localhost', 'root', '', 'cms_db'); } public function __destruct() { self::dbclose($this->dbc);
class Database
{
//Connection Variable
public $dbc;
public function __construct()
{
self::dbconnect('localhost', 'root', '', 'cms_db');
}
public function __destruct()
{
self::dbclose($this->dbc);
}
//Function to Connect to Database
public function dbconnect($host, $user, $pass, $db)
{
//Use Double Quotes in order to pass the value
$this->dbc = mysqli_connect("$host", "$user", "$pass", "$db") or die("Error in DB connection");
if ($this->dbc) {
return $this->dbc;
} else {
echo 'Cannot Connect to Database,Contact Your Admin';
}
} //end of Database Connect
//takes a mysql row set and returns an associative array, where the keys
//in the array are the column names in the row set. If singleRow is set to
//true, then it will return a single row instead of an array of rows.
public function processRowSet($rowSet, $singleRow = false)
{
$resultArray = array();
while ($row = mysqli_fetch_assoc($rowSet)) {
array_push($resultArray, $row);
}
if ($singleRow === true)
return $resultArray[0];
return $resultArray;
}
public function queryselect($field, $where, $table)
{
$selquery = "select $field from $table $where";
$result = mysqli_query($this->dbc, $selquery);
//echo Query If required to check
echo $selquery;
//checking for the number of rows and updating it as required
if (mysqli_num_rows($result) == 1)
return $this->processRowSet($result, true);
else
return $this->processRowSet($result);
} //Input valid
//insert operation
//Inserts a new row into the database.
//takes an array of data, where the keys in the array are the column names
//and the values are the data that will be inserted into those columns.
//$table is the name of the table.
public function insert($data, $table)
{
$columns = "";
$values = "";
foreach ($data as $column => $value) {
$columns .= ($columns == "") ? "" : ", ";
$columns .= $column;
$values .= ($values == "") ? "" : ", ";
$values .= "'" . $value . "'";
}
$sql = "insert into $table ($columns) values ($values)";
//you can echo and check what exactly is happening thats it. so nice
//echo $sql;
$result = mysqli_query($this->dbc, $sql);
return $result;
//return the ID of the user in the database.
// return mysqli_insert_id();
} //End of Insert Function
//Updates a current row in the database.
//takes an array of data, where the keys in the array are the column names
//and the values are the data that will be inserted into those columns.
//$table is the name of the table and $where is the sql where clause.
public function update($data, $table, $where)
{
$out = array();
foreach ($data as $column => $value) {
array_push($out, "$column='$value'");
}
$set = implode(', ', $out);
$sql = "UPDATE $table SET $set $where";
$result = mysqli_query($this->dbc, $sql);
echo $sql;
if ($result) {
return true;
} //end of valid if
} //end of update
//Deletes a current row in the database.
//$table is the name of the table and $where is the sql where clause.
public function delete($table, $where)
{
$sql = "DELETE from $table $where";
$result = mysqli_query($this->dbc, $sql);
//Delete Query can be echoed to check
//echo $sql;
if ($result) {
return true;
} //end of valid if
} //end of update
//Function to close the Database Connection
public function dbclose($dbc)
{
mysqli_close($dbc);
} //end of dbclose
} //end of Database class
我所写的Crud方法是否正确/最好的方法如果不正确,那么如何才能做到这一点,哪一种是最好的方法。实现可能因开发人员而异,因为每个人都有自己的解释方式 但是,作为一种良好的实践,在各种框架中通常采用的做法是创建一个不同的类来管理连接和执行SQL查询 例如。 您可以使用mysql、mysqli或PDO创建一个处理DB连接的类 接下来,创建一个抽象类,该类应定义各种CRUD操作,并为该操作调用DB类
现在,您可以创建任意多个类,您需要扩展基类,只需更改其中的表名。这个问题似乎是离题的,因为它是关于yes friends的,但我想知道我是否以正确的方式编写它,而不是直接投入生产并发现问题。我想知道这是否是正确的方式,因为我是PHP OOP新手,对于初学者来说,最经常出现的问题是我写的对不对,这就是我想要你们帮我解决的问题抽象类的概念很好,正如你所说,当你将一个类抽象化时,只执行所需的函数,这也提高了性能,并且避免了不必要的执行其他函数,我会重新组织我的代码,非常感谢