0

(I apologize in advance if this questions is posted in an improper or non-optimal venue.)

I am trying to compare the version number of the database schema from the table below with that of $oc_db_version from version.php.

DROP TABLE IF EXISTS `oc_config`;
CREATE TABLE `oc_config` (
  `key` varchar(80) NOT NULL,
  `value` varchar(80) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=COMPACT;

INSERT INTO `oc_config` (`key`, `value`) VALUES
('api_key', 'pzPgYOO7vz1WxgfD5913BMp5RYX8wpZPdp2KN5pdx78MRscT2X65zrmgfBQXCpQG'),
('oc_db_version',   '999');

I can't seem to get it working. I either get something to the effect of "PDOStatement cannot be converted to int" or I get bool(true). Below it the code which I am currently using.

   try{
        $pdo = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASSWORD);
    } catch(PDOException $ex)
    {
        $_SESSION['error'] = "Could not connect -> ".$ex->getMessage();
        $_SESSION['error_blob'] = $ex;
        header('Location: '.BASE_URL.'/oc-content/plugins/error/index.php');
        die();
    }

    $stmt = $pdo->query("SELECT value FROM ".DB_PREFIX."config WHERE `key` = 'oc_db_version'");
    $result = $stmt->fetch(PDO::FETCH_ASSOC);

    if (!$result['value'])
    {
        header('Location: '.BASE_URL.'/oc-content/plugins/error/index.php');
        die();
    }

    if ( $result['value'] != $oc_db_version || $result['value'] > $oc_db_version )
    {
          die("Database version mimatch. Please upgrade.");
    }
    else{ 
        // Do Nothing
    };
    $pdo = null;

I realize the code above could do with refactoring, some are probably cringing reading it and for that I apologize. I'd just like to get it working.

Phill Fernandes
  • 57
  • 3
  • 11
  • The single `|` should be a double `||` or maybe even the keyword `or`. And the `<=` should be a `<` because otherwise you will complain about the version even if it is equal. – Progman Oct 19 '19 at 18:41
  • @Progman, I made the adjustments you suggested but am still not getting the behaviour desired. Neither is PHP posting errors in it's logs. – Phill Fernandes Oct 20 '19 at 22:04
  • Use normal debugging tools like `var_dump()` to print the values you have and check them. You can use `var_dump($result, $result['value'], $oc_db_version);` to check every value going into the `if()` statement. Remember to change the error logging (error reporting) to see error messages which are normally hidden. – Progman Oct 20 '19 at 22:14
  • I took out the `if (!$result['value']){}` which is generating the error page. The output I get from your suggestion, @Progman, `bool(false) NULL int(999)`. Not quite sure what I am doing wrong here. – Phill Fernandes Oct 21 '19 at 01:53
  • Something went wrong with the request. From the docs (fetch): The return value of this function on success depends on the fetch type. In all cases, FALSE is returned on failure. – jperl Oct 21 '19 at 06:01
  • Possible duplicate of [PDO::Query() returning false](https://stackoverflow.com/questions/13074492/pdoquery-returning-false) – Progman Oct 21 '19 at 17:58

0 Answers0