0
        <?php

        class CMySQL {

            // variables
            var $sDbName;
            var $sDbUser;
            var $sDbPass;

            var $vLink;

            // constructor

            public function CMySQL(){
                $this->engine = 'mysql';
                $this->host = 'Localhost';
                $this->database = 'api';
                $this->user = 'root';
                $this->pass = '';
                $dns = $this->engine.':dbname='.$this->database.";host=".$this->host;
                 $this->vLink = $dns;


            }
            // return one value result
            function getOne($query, $index = 0) {
                if (! $query)
                    return false;
                $res = mysql_query($query);
                $arr_res = array();
                if ($res && mysql_num_rows($res))
                    $arr_res = mysql_fetch_array($res);
                if (count($arr_res))
                    return $arr_res[$index];
                else
                    return false;
            }

            // executing sql
            function res($query, $error_checking = true) {
                if(!$query)
                    return false;
                $res = $this->vLink;
                if (!$res)
                    $this->error('Database query error', false, $query);
                return $res;
            }

            // return table of records as result in pairs
            function getPairs($query, $sFieldKey, $sFieldValue, $arr_type = MYSQL_ASSOC) {
                if (! $query)
                    return array();

                $res = $this->res($query);
                $arr_res = array();
                if ($res) {
                    while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
                        $arr_res[$row[$sFieldKey]] = $row[$sFieldValue];
                    }
                    mysql_free_result($res);
                }
                return $arr_res;
            }

            // return table of records as result
            function getAll($query, $arr_type = MYSQL_ASSOC) {
                if (! $query)
                    return array();

                if ($arr_type != MYSQL_ASSOC && $arr_type != MYSQL_NUM && $arr_type != MYSQL_BOTH)
                    $arr_type = MYSQL_ASSOC;

                $res = $this->res($query);
                $arr_res = array();
                if ($res) {
                    while ($row = mysql_fetch_array($res, $arr_type))
                        $arr_res[] = $row;
                    mysql_free_result($res);
                }
                return $arr_res;
            }

            // return one row result
            function getRow($query, $arr_type = MYSQL_ASSOC) {
                if(!$query)
                    return array();
                if($arr_type != MYSQL_ASSOC && $arr_type != MYSQL_NUM && $arr_type != MYSQL_BOTH)
                    $arr_type = MYSQL_ASSOC;
                $res = $this->res ($query);
                $arr_res = array();
                if($res && mysql_num_rows($res)) {
                    $arr_res = mysql_fetch_array($res, $arr_type);
                    mysql_free_result($res);
                }
                return $arr_res;
            }

            // escape
            function escape($s) {
                return mysql_real_escape_string($s);
            }

            // get last id
            function lastId() {
                return mysql_insert_id($this->vLink);
            }

            // display errors
            function error($text, $isForceErrorChecking = false, $sSqlQuery = '') {
                echo $text; exit;
            }
        }

        $GLOBALS['MySQL'] = new CMySQL();
        ?>

I'm learning creating APIs using PHP and wish to convert "mysql_fetch_array and mysql_free_result" to PDO and allow multiple parameters as given. The error am getting for the above code is

mysql_fetch_array() expects parameter 1 to be resource, string given

and

mysql_free_result() expects parameter 1 to be resource, string given

How can I work around this code I came across with..

mountain
  • 106
  • 1
  • 14
  • 2
    You don't "work around" errors when you can fix them. You ommitted a description of the invoking code and the line number the error occurred at. Your problem arises from the fact you have no error checking in your code. – symcbean Sep 11 '17 at 12:44
  • Where are you using PDO? Don't use `mysql_*` with PDO. – chris85 Sep 11 '17 at 12:50
  • The/one answer to your database adapter class would be [the answer](https://stackoverflow.com/questions/46014772/return-multiple-response-data-in-one-response/46018999#46018999) that I wrote not so long time ago. Check the "EDIT" part. Good luck. –  Sep 11 '17 at 15:49

1 Answers1

0

You are missing the connection logic, I see that you commented this line:

//parent::CMySQL( $dns, $this->user, $this->pass );

However seems that you do not have an parent class for this one.

Check this link

zx485
  • 28,498
  • 28
  • 50
  • 59
king
  • 41
  • 2
  • 1
    Should use the english link, http://php.net/manual/en/function.mysql-connect.php, and also advice against using that. – chris85 Sep 11 '17 at 12:51
  • That line is not necessary as it just one function/class with a preceding one. – mountain Sep 11 '17 at 12:52