我想把我用mysql做的这段小代码转换成mysqli,任何可以帮助它的人都会受到赞赏。我尝试了几次但是我失败了,但是我需要用mysqli来做,因为将来mysql会被折旧。 。提前谢谢你。
<?php
CLASS DB_Class {
//database selection classes
function DB_Class($dbname, $username, $password) {
$this->db = mysql_connect ('host', 'guest', 'dbacess')
or die ("Unable to connect to Database Server");
mysql_select_db ($dbname, $this->db) or die ("Could not select database");
}
function query($sql) {
$result = mysql_query ($sql, $this->db) or die ("Invalid query: " . mysql_error());
return $result;
}
function fetch($sql) {
$data = array();
$result = $this->query($sql);
while($row = MYSQL_FETCH_ASSOC($result)) {
$data[] = $row;
}
return $data;
}
function getone($sql) {
$result = $this->query($sql);
if(MYSQL_NUM_ROWS($result) == 0)
$value = false;
else
$value = MYSQL_RESULT($result, 0);
return $value;
}
}
?>
答案 0 :(得分:0)
查询方法的冲突可能会抓住你,我已经注意到了这个_query的重命名。我测试了每种方法以确保它们也能正常工作。如果您有任何问题,请告诉我。
class DB_Class {
//database selection classes
private $db;
function DB_Class($dbname, $host, $username, $password) {
$this->db = new mysqli($host, $username, $password, $dbname);
}
function _query($sql) {
$result = $this->db->query($sql) or die ("Invalid query: " . mysqli_error());
return $result;
}
function fetch($sql) {
$data = array();
$result = $this->_query($sql);
while($row = mysqli_fetch_array($result)) {
$data[] = $row;
}
return $data;
}
function getone($sql) {
$result = $this->_query($sql);
if(mysqli_num_rows($result) == 0)
$value = false;
else
$value = $result->fetch_row();
return $value;
}
}