Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class UrlsMemory
- {
- const CREATE_SCHEMA_URLS_MEMORY =
- "CREATE SCHEMA IF NOT EXISTS sch_urls_mem;";
- const CREATE_TABLE_URLS =
- "CREATE TABLE IF NOT EXISTS sch_urls_mem.tUrls(
- _id INT NOT NULL AUTO_INCREMENT,
- url TEXT NOT NULL,
- anchor TEXT NOT NULL,
- PRIMARY KEY(_id));";
- const DROP_TABLE_URLS =
- "DROP TABLE IF EXISTS sch_urls_mem.tUrls;";
- private $mHost, $mUser, $mPass, $mPort;
- private $mLastErrorCode, $mLastErrorMsg;
- private $mErrorCodes, $mErrorMsgs;
- private $mDb; //fundamental!
- const DEFAULT_HOST = "localhost";
- const DEFAULT_USER = "test";
- const DEFAULT_PASS = "1234";
- const DEFAULT_PORT = 3306;
- public function __construct(){
- $this->mHost = self::DEFAULT_HOST;
- $this->mUser = self::DEFAULT_USER;
- $this->mPass = self::DEFAULT_PASS;
- $this->mPort = self::DEFAULT_PORT;
- $this->mDb = mysqli_connect(
- $this->mHost,
- $this->mUser,
- $this->mPass,
- "",
- $this->mPort
- );
- $this->mLastErrorCode = mysqli_connect_errno();
- $this->mLastErrorMsg = mysqli_connect_error();
- $this->mErrorCodes[] = $this->mLastErrorCode;
- $this->mErrorMsgs[] = $this->mLastErrorMsg;
- $this->errorFb();
- }//__construct
- private function errorFb(){
- if ($this->mLastErrorCode!==0){
- $strMsg = sprintf(
- "Last error code: %d\n%s",
- $this->mLastErrorCode,
- $this->mLastErrorMsg
- );
- echo $strMsg;
- }
- }//errorFb
- private function updateErrors(){
- $this->mLastErrorCode = mysqli_errno($this->mDb);
- $this->mLastErrorMsg = mysqli_error($this->mDb);
- $this->mErrorCodes[] = $this->mLastErrorCode;
- $this->mErrorMsgs[] = $this->mLastErrorMsg;
- }//updateError
- public function install(){
- if ($this->mDb){
- $this->mDb->query(self::CREATE_SCHEMA_URLS_MEMORY);
- $this->updateErrors();
- $this->errorFb();
- $this->mDb->query(self::CREATE_TABLE_URLS);
- $this->updateErrors();
- $this->errorFb();
- }//if
- }//install
- public function insertUrl(
- string $pHref,
- string $pAnchor
- ){
- $q = "INSERT INTO sch_urls_mem.tUrls VALUES (".
- "null, '$pHref', '$pAnchor');";
- $this->mDb->query($q);
- $this->updateErrors();
- $this->errorFb();
- }//insertUrl
- public function selectAllUrls(){
- //$q = "SELECT * FROM sch_urls_mem.tUrls;";
- $q = "SELECT _id, url, anchor FROM sch_urls_mem.tUrls;";
- $r = $this->mDb->query($q);
- $this->updateErrors();
- $this->errorFb();
- /*
- * MYSQLI_ASSOC
- * $aAllRecords[0] => [
- * "_id" => id do registo
- * "url" => url do registo
- * "anchor" => anchor do registo
- * ]
- *
- * $aAllRecords[0] => [
- * 0 => id do registo
- * 1 => url do registo
- * 0 => anchor do registo
- * ]
- */
- $aAllRecords =
- mysqli_fetch_all(
- $r,
- MYSQLI_ASSOC
- );
- return $aAllRecords;
- }//selectAllUrls
- }
- $o = new UrlsMemory();
- $o->install();
- $o->insertUrl("http://xpto.com", "bla bla");
- $all = $o->selectAllUrls();
- var_dump ($all);
Advertisement
Add Comment
Please, Sign In to add comment