Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 2 FirstFirst 12
Results 16 to 19 of 19

Thread: PDO INT vrs STR

  1. #16
    Senior Coder durangod's Avatar
    Join Date
    Nov 2010
    Location
    East Texas USA
    Posts
    2,220
    Thanks
    378
    Thanked 60 Times in 58 Posts
    done, sorry about that
    If a php file only has php code within it you do not need to use the closing php tag
    A good way to remember objects from arrays is you shoot objects with arrows Example: $name->id; then Arrays are $name['id'];
    durangod is short for durango dave

  2. #17
    Senior Coder durangod's Avatar
    Join Date
    Nov 2010
    Location
    East Texas USA
    Posts
    2,220
    Thanks
    378
    Thanked 60 Times in 58 Posts
    Sorry for my outward venting, i get more mad at myself than anything because i used to be a zip at programming, now it takes forever for me to understand it. Traumatic Brain Injury, its not all its cracked up to be trust me.

    Anyway LT pointed out that there was a security hole in my connection glass. So i figured i would fix that and then maybe learn something in the process, and i did.

    I sat down with some reading and some videos and i just did some testing on the site, i tested this and that, and what happend when i did something, and did not do something. One of the hardest things about this are the terms used. Singleton lol, why not just call it a single instance limit. Instantiation, why not call it a class instance. Too many words that confuse the already confused mind when it comes to classes.

    Anyway in the process today i got it work and its safe now. Through my testing i knew there were 3 things i needed to do, one was to remove the function from outside the class, and two was to secure the class, and three was to find a way to access the protected class from another file.

    The answer hit me when the instructor was talking about protected (and strangly alot of the other media i read was not this specific). THe instructor noted that on a protected class it can be accessed by the class, the child class, and also by the extended class. That is when the light bulb came on in my head and i could see the code exactly as i needed to do it in my head.

    I moved the makeConnection function inside the Dbpdo class, made the functions protected, that pluged up that hole. Then in the db sql files that need the connection i just extended Dbpdo to allow that db class to access the Dbpdo class. Then i just added this to the top of my db class files

    In the construct of each file i put

    PHP Code:

    $this
    ->pdo $this->makeConnection
    that gave me the connection without making anything global or having the function exposed to being run using Dbpdo::connect{} from outside the class, so that hole is plugged.

    And now i understand alittle more about how to move instances around safely by using extend.

    Now i am happy with how it is, its safe and its correctly done.
    Last edited by durangod; Jul 1st, 2019 at 09:32 AM.
    If a php file only has php code within it you do not need to use the closing php tag
    A good way to remember objects from arrays is you shoot objects with arrows Example: $name->id; then Arrays are $name['id'];
    durangod is short for durango dave

  3. #18
    Senior Coder benanamen's Avatar
    Join Date
    Oct 2015
    Posts
    1,624
    Thanks
    11
    Thanked 164 Times in 159 Posts
    its safe and its correctly done.
    I would suggest posting your current attempt. Something sounds not right.
    To save time, lets just assume I am almost never wrong.

    The XY Problem
    The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.

    Make A Donation https://www.paypal.me/KevinRubio

  4. #19
    Senior Coder durangod's Avatar
    Join Date
    Nov 2010
    Location
    East Texas USA
    Posts
    2,220
    Thanks
    378
    Thanked 60 Times in 58 Posts
    Sure NP...

    here is the new code in the config file

    PHP Code:

    //init stuff
    define('WSS_SERVER',  $servername); 
    define('WSS_USER',    $username); 
    define('WSS_PASS',    $password); 
    define('WSS_DBNAME',  $dbname); 
    define('WSS_CHARSET'$charset);

    require_once(
    dirname(dirname(__DIR__)).'/includes/classes/db_classes/db_connect.php'); 

    here is the connection file

    PHP Code:

    //connect to db  

    class Dbpdo{
        
             private 
    $dsn;
             private 
    $pdo;
        
             protected function 
    __construct()
             {
                 
                
           
             }
        
           
             protected function 
    connect()
             {
           
               
               try { 
                    
                      
    //pdo connection 
                      
    $dsn "mysql:host=".WSS_SERVER.";dbname=".WSS_DBNAME.";charset=".WSS_CHARSET
                      
    $pdo = new PDO($dsn,WSS_USERWSS_PASS);  
                      
    $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,false); 
                      
    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODEPDO::FETCH_ASSOC); 
                      
    $pdo->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION); 
                      return 
    $pdo
                    
               } catch(
    PDOException $e
                 { 
                     
                     
    // log the error so it may be looked at later if necessary  
                     
    error_log($e->getMessage());
                     
                     
    $filepath WBS_STORE_BASE."connection_error.php";
                     
                     
    header("location: ".$filepath);
                     
                     exit;
                     
                  }
    //close catch  
                
             
    }//close function connect 
             
                            
            
    protected function makeConnection()
            {
      
              
    //init arrays
              
    $newpdo = array();
              
    $newcon = array();
      
              
    //run connection
              
    $newpdo = new Dbpdo;
              
    $newcon =  $this->connect();

              return 
    $newcon;

            }
    //close function makeConnection

         
    }//close class 

    here is a sample of the sql file

    PHP Code:

    class Dbpcat extends Dbpdo {
        
        
          private 
    $pdo
        
        
          public function 
    __construct()
          {
              
            
    $this->pdo $this->makeConnection();
            
          }
       
          
          
         public function 
    getCatagoryNameByCatId($value)
         {
             
             
    $cid = (int) $value//cat id

             
    $sql="SELECT `catname` FROM `wbs_product_categories` WHERE `id` = :c";
             
    $stmt $this->pdo->prepare($sql);
             
    $stmt->execute([':c'=>$cid]);
             
             
    $result $stmt->fetch();
           
           if(
    is_array($result))
           {
           
             return 
    $result['catname']; 
             
           }else{
               
                 return 
    false;
                 
                }
    //close else

         
    }//close function getCatagoryNameByCatId 
    If a php file only has php code within it you do not need to use the closing php tag
    A good way to remember objects from arrays is you shoot objects with arrows Example: $name->id; then Arrays are $name['id'];
    durangod is short for durango dave


 
Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •