am_dot_com

ACA 20201215

Dec 15th, 2020 (edited)
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.49 KB | None | 0 0
  1. <?php
  2.  
  3. class UrlsMemory
  4. {
  5.     const CREATE_SCHEMA_URLS_MEMORY =
  6.         "CREATE SCHEMA IF NOT EXISTS sch_urls_mem;";
  7.  
  8.     const CREATE_TABLE_URLS =
  9.         "CREATE TABLE IF NOT EXISTS sch_urls_mem.tUrls(
  10.        _id INT NOT NULL AUTO_INCREMENT,
  11.        url TEXT NOT NULL,
  12.        anchor TEXT NOT NULL,
  13.        PRIMARY KEY(_id));";
  14.  
  15.     const DROP_TABLE_URLS =
  16.         "DROP TABLE IF EXISTS sch_urls_mem.tUrls;";
  17.  
  18.     private $mHost, $mUser, $mPass, $mPort;
  19.     private $mLastErrorCode, $mLastErrorMsg;
  20.     private $mErrorCodes, $mErrorMsgs;
  21.     private $mDb; //fundamental!
  22.  
  23.     const DEFAULT_HOST = "localhost";
  24.     const DEFAULT_USER = "test";
  25.     const DEFAULT_PASS = "1234";
  26.     const DEFAULT_PORT = 3306;
  27.  
  28.     public function __construct(){
  29.         $this->mHost = self::DEFAULT_HOST;
  30.         $this->mUser = self::DEFAULT_USER;
  31.         $this->mPass = self::DEFAULT_PASS;
  32.         $this->mPort = self::DEFAULT_PORT;
  33.  
  34.         $this->mDb = mysqli_connect(
  35.             $this->mHost,
  36.             $this->mUser,
  37.             $this->mPass,
  38.             "",
  39.             $this->mPort
  40.         );
  41.         $this->mLastErrorCode = mysqli_connect_errno();
  42.         $this->mLastErrorMsg = mysqli_connect_error();
  43.         $this->mErrorCodes[] = $this->mLastErrorCode;
  44.         $this->mErrorMsgs[] = $this->mLastErrorMsg;
  45.  
  46.         $this->errorFb();
  47.     }//__construct
  48.  
  49.     private function errorFb(){
  50.         if ($this->mLastErrorCode!==0){
  51.             $strMsg = sprintf(
  52.             "Last error code: %d\n%s",
  53.             $this->mLastErrorCode,
  54.             $this->mLastErrorMsg
  55.             );
  56.             echo $strMsg;
  57.         }
  58.     }//errorFb
  59.  
  60.     private function updateErrors(){
  61.         $this->mLastErrorCode = mysqli_errno($this->mDb);
  62.         $this->mLastErrorMsg = mysqli_error($this->mDb);
  63.         $this->mErrorCodes[] = $this->mLastErrorCode;
  64.         $this->mErrorMsgs[] = $this->mLastErrorMsg;
  65.     }//updateError
  66.  
  67.     public function install(){
  68.         if ($this->mDb){
  69.             $this->mDb->query(self::CREATE_SCHEMA_URLS_MEMORY);
  70.             $this->updateErrors();
  71.             $this->errorFb();
  72.  
  73.             $this->mDb->query(self::CREATE_TABLE_URLS);
  74.             $this->updateErrors();
  75.             $this->errorFb();
  76.         }//if
  77.     }//install
  78.  
  79.     public function insertUrl(
  80.         string $pHref,
  81.         string $pAnchor
  82.     ){
  83.         $q = "INSERT INTO sch_urls_mem.tUrls  VALUES (".
  84.         "null, '$pHref', '$pAnchor');";
  85.  
  86.         $this->mDb->query($q);
  87.  
  88.         $this->updateErrors();
  89.         $this->errorFb();
  90.     }//insertUrl
  91.  
  92.     public function selectAllUrls(){
  93.         //$q = "SELECT * FROM sch_urls_mem.tUrls;";
  94.         $q = "SELECT _id, url, anchor FROM sch_urls_mem.tUrls;";
  95.  
  96.         $r = $this->mDb->query($q);
  97.         $this->updateErrors();
  98.         $this->errorFb();
  99.  
  100.         /*
  101.          * MYSQLI_ASSOC
  102.          * $aAllRecords[0] => [
  103.          *  "_id" => id do registo
  104.          *  "url" => url do registo
  105.          *  "anchor" => anchor do registo
  106.          * ]
  107.          *
  108.          * $aAllRecords[0] => [
  109.          *  0 => id do registo
  110.          *  1 => url do registo
  111.          *  0 => anchor do registo
  112.          * ]
  113.          */
  114.         $aAllRecords =
  115.             mysqli_fetch_all(
  116.                 $r,
  117.                 MYSQLI_ASSOC
  118.             );
  119.  
  120.         return $aAllRecords;
  121.     }//selectAllUrls
  122.  
  123.  
  124. }
  125.  
  126. $o = new UrlsMemory();
  127. $o->install();
  128. $o->insertUrl("http://xpto.com", "bla bla");
  129. $all = $o->selectAllUrls();
  130. var_dump ($all);
Advertisement
Add Comment
Please, Sign In to add comment