mysqli och DAL

Testfall

$mysqli = new mysqli("localhost", "root", "", "BookStore");

$bookStore = new BookStore($mysqli);

$controller = new AddBookController($bookStore);

echo $controller->addBook();

BookStore.php

/**
	* @var BookDAL
	*/ 
	private $bookDAL;

	public function __construct(mysqli $dbConnection) {
		$this->bookDAL = new BookDAL($dbConnection);

		$this->books = $this->bookDAL->getAllBooks();
	}
/**
	* @param Book book The book to be added
	* @return boolean Return true if book was added, 
	*                        false if book already was in store...
	*/
	public function addBook(Book $book) {
		if ($this->isBookInStore($book))
			return false;

		//Add book to the end of the array
		$this->books[] = $book;
		$this->bookDAL->insertBook($book);

		return true;
	}

BookDAL.php

<?php

require_once("Book.php");
/*
* Data Acess Layer (DAL) for Books
*/
class BookDAL {
	private static $tableName = "book";

	/**
	* @var mysqli 
	*/
	private $mysqli;

	/**
	* @param mysqli  http://www.php.net/manual/en/book.mysqli.php
	*/
	public function __construct(mysqli $mysqli) {
		$this->mysqli = $mysqli;
	}

	/**
	* @throws Exception on error
	*/
	public function createTable() {
      	$sql = "CREATE TABLE `" . self::$tableName . "` 
      		(
      			`pk` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, 
      			`author` VARCHAR(255),
      			`title` VARCHAR(255),
      			`isbn` VARCHAR(255)
      		)
      		ENGINE = MyISAM;";
      
      if ($this->mysqli->query($sql) === FALSE) {
          throw new Exception("'$sql' failed " . $this->mysqli->error);
      }
    }

    /**
    * @param Book the book to insert
    * @throws Exception if something goes wrong.
    */
    public function insertBook(Book $book) {
		$sql = "INSERT INTO " . self::$tableName . " 
			(
				author, 
				title, 
				isbn
			) 
			VALUES(?, ?, ?)";

		//http://www.php.net/manual/en/mysqli-stmt.prepare.php
		$statement = $this->mysqli->prepare($sql);
		if ($statement === FALSE) {
			throw new Exception("prepare of $sql failed " . $this->mysqli->error);
		}

		//http://www.php.net/manual/en/mysqli-stmt.bind-param.php
		if ($statement->bind_param("sss", $book->author, 
										  $book->title, 
										  $book->isbn) === FALSE) {
			throw new Exception("bind_param of $sql failed " . $statement->error);
		}

		//http://www.php.net/manual/en/mysqli-stmt.execute.php
		if ($statement->execute() === FALSE) {
			throw new Exception("execute of $sql failed " . $statement->error);
		}

    }

	/**
    * @return array of Book objects
    */
    public function getAllBooks() {
    	$ret = array();

    	$sql = "SELECT 
    				author, 
					title, 
					isbn FROM " . self::$tableName . ";";

		//http://www.php.net/manual/en/mysqli-stmt.prepare.php
		$statement = $this->mysqli->prepare($sql);
		if ($statement === FALSE) {
			throw new Exception("prepare of $sql failed " . $this->mysqli->error);
		}

		//http://www.php.net/manual/en/mysqli-stmt.execute.php
		if ($statement->execute() === FALSE) {
			throw new Exception("execute of $sql failed " . $statement->error);
		}

		//http://www.php.net/manual/en/mysqli-stmt.get-result.php
		$result = $statement->get_result();
			
		//http://www.php.net/manual/en/mysqli-result.fetch-array.php
		while ($object = $result->fetch_array(MYSQLI_ASSOC))
        {
        	$ret[] = new Book($object["author"], 
        					  $object["title"], 
        					  $object["isbn"]);


           	
        }

        return $ret;
    }

}


Welcome to CoursePress

en utav Linnéuniversitets lärplattformar. Som inloggad student kan du kommunicera, hålla koll på dina kurser och mycket mer. Du som är gäst kan nå de flesta kurser och dess innehåll utan att logga in.

Läs mer lärplattformar vid Linnéuniversitetet

Student account

To log in you need a student account at Linnaeus University.

Read more about collecting your account

Log in LNU