1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Explain code

Discussion in 'PHP' started by piropeator, Dec 1, 2015.

  1. #1
    Hello. I have a PHP project using MySQL and ADODB.
    define('ADODB_BASEFILE', APP_BASEDIR . '/lib/adodb518/adodb.inc.php');
    define('DB_HOST', 'localhost');
    define('DB_USER', 'root');
    define('DB_KEY' , 'key');
    define('DB_DATA', 'database');
    define('DB_TYPE', 'mysqli');
    
    PHP:
    My ADODB class connection is:
    <?php
    require_once ADODB_BASEFILE;
    class ConexionDB {
        public $dbLink;
        public function __construct(){
            $this->dbLink = ADONewConnection(DB_TYPE);
            $this->dbLink->SetFetchMode(ADODB_FETCH_ASSOC);
            $dbconnected = $this->dbLink->PConnect(DB_HOST, DB_USER, DB_KEY, DB_DATA);
            if (!$dbconnected){
                Debug::println('connection error database');
                exit(0);
            }
        }
        function __destruct(){
        }
    }
    ?>
    PHP:
    This code is not mine.
    My questions are:
    1) What mean class Debug
    Debug::println(...)
    PHP:
    2) What mean ADODB_FETCH_ASSOC
    $this->dbLink->SetFetchMode(ADODB_FETCH_ASSOC);
    PHP:
    Thanks.
     
    piropeator, Dec 1, 2015 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Okay, first off, I have no idea what ADODB is - but if you're using it, shouldn't you know what it does? Read the documentation, I'm quite sure it will tell you what these things are/mean.
    Secondly, why are you using an abstraction layer you yourself haven't even made, to connect to a database? Why not just do it directly, or write a helper-class if you need to?
    Third, DB-variables as CONSTANTS? Really? Have we learned nothing? NOT a good idea. See DB-exploits in Wordpress as an example, together with limiting scope among other things.
    Fourth, "Debug" is obviously a logging-class, probably created by the ADODB-people. I'm guessing it either outputs the error to the screen, or logs it in a log-file somewhere. Since I haven't looked at ADODB, I've no idea where that class is present.
    Fifth, the ADODB_FETCH_ASSOC is another constant setting the fetch-mode when fetching stuff from the database to ASSOC (associated) - which means you'll get key/value pairs, instead of numeric/value pairs, or both.
     
    PoPSiCLe, Dec 1, 2015 IP
    sarahk likes this.
  3. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #3
    Well, here in my new job there is a project, working and need changes, but it use ADODB for connect to any database, in this project MySQL.
    And what structure do you recommend for my project?
     
    piropeator, Dec 1, 2015 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #4
    Shouldn't you
    • be telling your supervisor you need training before undertaking this task?
    • asking your supervisor what the company standards are?
    We, and especially @PoPSiCLe, can give you awesome advice which would be appropriate for a hobbyist or freelancer. As an employee you have a responsibility to find out how things are done in the company and to follow the company protocols. If the company standards are crappy then you need to request time to define them.
     
    sarahk, Dec 1, 2015 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #5
    While I do not use that outdated train-wreck known as ADODB that servers no legitimate purpose any time after PDO came into existence (since it tries and fails to do what PHP can now do itself) -- and I pretty much have to agree with PoPSiCLe that the code is special -- in the same way some olympics are special...

    Let's actually answer your questions...

    Obviously some form of logging class that's printing something to.. somewhere. A Database? A Log file? Without seeing the entire codebase and knowing what else is running impossible to say.

    Much like every other FETCH_ASSOC constant to be found in PHP, we can pretty much assume this means to only return the result as an associative array instead of a indexed array or combination of both. In MOST systems "both" is the default which is a huge waste of memory and processing time.

    Associative array:
    $test = [ 'a' => 3, 'b' => 5, 'c' => 7 ];
    echo $test['b']; // outputs 5

    Indexed array:
    $test = [3, 5, 7];
    echo $test[1]; // outputs 5

    The reason it outputs 5 is they are auto-numbered starting at zero, so that's kind-of the same as saying:

    $test[0] = 3;
    $test[1] = 5;
    $test[2] = 7;

    A "Both" array would be the same as listing all the same data twice:

    $test[0] = 3;
    $test['a'] = 3;
    $test[1] = 5;
    $test['b'] = 5;
    $test[2] = 7;
    $test['c'] = 7;

    Twice as much memory, but can be indexed either way... Pain in the ass too if you want to delete specific values for it as you need to track both keys IF you can.

    The mouth-breathing halfwit nonsense known as ADOdb which wastes a slew of code replicating behaviors already built into PHP allows you to set the result type ONCE at the start instead of having to do so on every query. Apart from that it's functionally identical to passing the same value to a ::fetch in PDO or mySQLi.

    See the "fetch types" under PDOStatement::fetch which are similar in function.
    http://php.net/manual/en/pdostatement.fetch.php

    ADOdb almost made sense a decade ago when the only database access method was the old (now deprecated) mysql_ functions. Today with PDO a reality it serves no legitimate purpose, and I'd advise your employers to kick it to the curb ASAP as it's NOT doing a thing for you apart from encouraging sloppy and insecure methodology.

    Really given how ADOdb works, conversion to PDO should be a no-brainer though you would have to implement your own markup output since it does try to handle some things like pagination, tabular output and menu generation for you. (badly)
     
    deathshadow, Dec 1, 2015 IP
    sarahk likes this.
  6. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #6
    Well, thanks, yes I was reading about alternatives about adodb, and PDO is a good option. But why you say "in PDO or MySQLi" both of them are completely different?

    And how I can to made a class to connect to database for my project?
    What structure do you recommend for my project?
    Thanks.
     
    piropeator, Dec 2, 2015 IP
  7. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #7
    I don't have a problem with you using an existing library but I do recommend that you read through it and understand what the code is doing and ensure that it's not doing anything nefarious - like sending your login details to a log file or another website. That's easier said than done, though, as some are very complex however you'll get better protection from sql injections etc if you use the know-how of someone more expert than yourself.
     
    sarahk, Dec 2, 2015 IP
  8. iamgaurav

    iamgaurav Member

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    46
    #8
    You may wanna read the documentation from here http://adodb.sourceforge.net/
     
    iamgaurav, Dec 8, 2015 IP
  9. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #9
    Well, I'll stop using adodb and I'm reading about PDO. Thanks.
     
    piropeator, Dec 12, 2015 IP