我试图在PHP中使用CRUD方法来处理所有数据库活动,我在下面发布我的代码
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方法是否正确/最好的方法,如果不是这样,如何做到这是最好的方法..
答案 0 :(得分:2)
实施可能因开发人员而异,因为每个人都有自己的解释方式。
但是,作为一种良好的做法,通常在各种框架中实践的是创建一个不同的类来管理连接和执行SQL查询。
例如 您可以使用mysql或mysqli或PDO创建一个处理数据库连接的类。
接下来创建一个抽象类,该类应定义各种CRUD操作并调用操作的DB类。
现在,您可以创建任意数量的类,您希望扩展基类,只需更改其中的表名。