使用mysqli查询无法正常工作

时间:2013-12-07 01:59:27

标签: php mysql database mysqli

我正在学习PHP语言,我正在用MVC方法接近这个新项目。我为很多对象编写了单独的DAO类,但特别是我的数据库连接有问题。我在使用数据库的其他项目中使用了相同的确切代码,但它工作得很好但是没有这个项目的运气。 这是我到目前为止测试的两个类的代码,并且addBook方法不起作用,因为它实际上不会将信息添加到数据库,但是没有语法错误。请提前告诉您。 数据库连接类

<?php
class DBConn {
private $dbc;
private $query;
private $result;

public function __construct() {
    $this -> dbc = mysqli_connect('localhost', 'root', '', 'library') or die("Error " . mysqli_error($this -> dbc));
}

public function connect() {
    if (is_resource($this -> dbc) && get_resource_type($this -> dbc) === 'mysql link') {
        echo "Connnected";
        return $this -> dbc;
    } else {
        $this -> dbc = mysqli_connect('localhost', 'root', '', 'library');
    }
}

public function close() {
    mysqli_close($this -> dbc);
}

function __destruct() {
    mysqli_close($this -> dbc);
}public function freeResult() {
    mysqli_free_result($this -> result);
}

public function setDbc($dbc) {
    $this -> dbc = $dbc;
}

/*
 * Sets the query to a modified version of the argument to prevent SQL injection.
 */
public function setQuery($query) {
    $this -> query = mysqli_real_escape_string($this -> dbc, trim($query));
}

public function setResult($result) {
    $this -> result = $result;
}

public function getDbc() {
    return $this -> dbc;
}

public function getQuery() {
    return $this -> query;
}

public function getResult() {
    return $this -> result;
}

/*
 * Executes set query. Query has already been modified to prevent SQl injection.
 * Sets the results of the query to the result variable.
 */
public function executeQuery() {
    $this -> result = mysqli_query($this -> dbc, $this -> query);
}

public function salting($password) {
    $salt1 = "@mhge7";
    $salt2 = "#2uqil";
    return (sha1("$salt1$password$salt2"));
}

}
?>

预订DAO课程

<?php
require_once ('Book.php');
require_once ('DBConn.php');

class BookDao {
 private $db_conn;

function __construct() {
    $this->db_conn=new DBConn;
}

public function addBook(Book & $book) {
    //if (isbnAvailable($book -> getIsbn())) {
        $this->db_conn->connect();
        $this->db_conn->connect();
        $title = $book->getTitle();
        $author = $book->getAuthor();
        $isbn = $book->getIsbn();
        $this->db_conn->connect();
        $this->db_conn -> setQuery("Insert into 'books'(ISBN,Title,Author) Values('$isbn','$title','$author')");
        $this->db_conn -> executeQuery();
        //$this->db_conn -> close();
        if($this->db_conn->getResult() == FALSE){
            echo "FALSE";
        }
        else {
            echo "TRUE";
        }

}

public function isbnAvailable($isbn) {
    $this->db_conn -> connect();
    $this->db_conn -> setQuery("Select isbn from books where ISBN = ' . $isbn . '");
    $this->db_conn -> executeQuery();
    $this->db_conn -> close();
    if (mysqli_num_rows($db_conn -> getResult())) {
        $this->db_conn -> freeResult();
        return false;
    } else {
        $this->db_conn -> freeResult();
        return true;
    }
}

}
?>

0 个答案:

没有答案