将mysql_fetch_array转换为PDO并具有多个参数

时间:2017-09-11 12:40:52

标签: php pdo

        <?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();
        ?>

我正在学习使用PHP创建API并希望将“mysql_fetch_array和mysql_free_result”转换为PDO并允许多个参数给定。上述代码的错误是

  

mysql_fetch_array()期望参数1为资源,字符串为

  

mysql_free_result()期望参数1为资源,字符串为

我如何处理我遇到的代码..

1 个答案:

答案 0 :(得分:0)

你错过了连接逻辑,我看到你注释了这一行:

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

但是,似乎您没有此类的父类。

Check this link