解决Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in

  • A+
所属分类:PHP

php 5个版本,5.2、5.3、5.4、5.5,怕跟不上时代,新的服务器直接上5.5,但是程序出现如下错误:Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in,看意思就很明了,说mysql_connect这个模块将在未来弃用,请你使用mysqli或者PDO来替代。

解决方法1:

禁止php报错

鉴于这个服务器都是给用户用的,有时候他们需要报错(...都是给朋友用的,^_^),不能这做,让他们改程序吧,看方案2.

解决方法2:

常用的php语法连接mysql如下

常用mysql建表SQL如下

解决方法三:

在php程序代码里面设置报警级别

Deprecated的问题就这样解决掉了,不过还是建议大家尽快取消mysql的用法,全部都走向mysqli或者mysqlnd等等。mysql确实是太不安全而且太老旧了。

weinxin
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~

评论已关闭!

目前评论:18   其中:访客  16   博主  0   引用   2

    • 追梦小窝 0

      :sad: 现在刚开始学PHP,总是学的落后版本的,环境配置的又是新的,真蛋疼啊 :cry:

      • ET 0

        $r = $db->pconnect( ‘p:’$cfg_db_hostname.$server, $cfg_db_username, $cfg_db_password, $cfg_db_name );

        请问这个怎么改

        • 绝缘材料 1

          确实。 抱着老掉牙的东西咋进步

          • lvxiongmao 9

            大哥帮我看看
            exec($sql) )return array();
            if( ! mysql_num_rows($result) )return array();
            $rows = array();
            while($rows[] = mysql_fetch_array($result,MYSQL_ASSOC)){}
            mysql_free_result($result);
            array_pop($rows);
            return $rows;
            }

            public function newinsertid()
            {
            return mysql_insert_id($this->conn);
            }

            public function showtables($tables)
            {
            return mysql_num_rows(mysql_query(“show tables like ‘%”.$tables.”%'”,$this->conn));
            }

            public function setlimit($sql, $limit)
            {
            return $sql. ” LIMIT {$limit}”;
            }

            public function exec($sql)
            {
            $this->arrSql[] = $sql;
            if( $result = mysql_query($sql, $this->conn) ){
            return $result;
            }else{
            if(mysql_error()!=”){
            syError(“{$sql}执行错误: ” . mysql_error());
            }else{
            return TRUE;
            }
            }
            }

            public function affected_rows()
            {
            return mysql_affected_rows($this->conn);
            }

            public function getTable($tbl_name)
            {
            return $this->getArray(“DESCRIBE {$tbl_name}”);
            }

            public function __construct($dbConfig)
            {
            $linkfunction = ( TRUE == $dbConfig[‘persistent’] ) ? ‘mysql_pconnect’ : ‘mysql_connect’;
            $this->conn = $linkfunction($dbConfig[‘host’].”:”.$dbConfig[‘port’], $dbConfig[‘login’], $dbConfig[‘password’]);
            if(!$this->conn || !mysql_select_db($dbConfig[‘database’], $this->conn)){
            echo ‘数据库无法链接,如果您是第一次使用,请先执行安装程序网润科技建站程序 m.cqbaidu.com‘;exit;
            }
            $this->exec(“SET NAMES UTF8”);
            if($this->version() > ‘5.0.1’) {
            $this->exec(“set sql_mode=””);
            }
            }
            public function version() {
            return mysql_get_server_info($this->conn);
            }

            public function __val_escape($value) {
            return ‘\”.$value.’\”;
            }

            public function __destruct()
            {
            if( TRUE != $GLOBALS[‘G_DY’][‘db’][‘persistent’] )@mysql_close($this->conn);
            }
            }

            • 谁能帮我吗 9

              能帮我改改吗?
              exec($sql) )return array();
              if( ! mysql_num_rows($result) )return array();
              $rows = array();
              while($rows[] = mysql_fetch_array($result,MYSQL_ASSOC)){}
              mysql_free_result($result);
              array_pop($rows);
              return $rows;
              }

              public function newinsertid()
              {
              return mysql_insert_id($this->conn);
              }

              public function showtables($tables)
              {
              return mysql_num_rows(mysql_query(“show tables like ‘%”.$tables.”%'”,$this->conn));
              }

              public function setlimit($sql, $limit)
              {
              return $sql. ” LIMIT {$limit}”;
              }

              public function exec($sql)
              {
              $this->arrSql[] = $sql;
              if( $result = mysql_query($sql, $this->conn) ){
              return $result;
              }else{
              if(mysql_error()!=”){
              syError(“{$sql}执行错误: ” . mysql_error());
              }else{
              return TRUE;
              }
              }
              }

              public function affected_rows()
              {
              return mysql_affected_rows($this->conn);
              }

              public function getTable($tbl_name)
              {
              return $this->getArray(“DESCRIBE {$tbl_name}”);
              }

              public function __construct($dbConfig)
              {
              $linkfunction = ( TRUE == $dbConfig[‘persistent’] ) ? ‘mysql_pconnect’ : ‘mysql_connect’;
              $this->conn = $linkfunction($dbConfig[‘host’].”:”.$dbConfig[‘port’], $dbConfig[‘login’], $dbConfig[‘password’]);
              if(!$this->conn || !mysql_select_db($dbConfig[‘database’], $this->conn)){
              echo ‘数据库无法链接,如果您是第一次使用,请先执行安装程序网润科技建站程序 m.cqbaidu.com‘;exit;
              }
              $this->exec(“SET NAMES UTF8”);
              if($this->version() > ‘5.0.1’) {
              $this->exec(“set sql_mode=””);
              }
              }
              public function version() {
              return mysql_get_server_info($this->conn);
              }

              public function __val_escape($value) {
              return ‘\”.$value.’\”;
              }

              public function __destruct()
              {
              if( TRUE != $GLOBALS[‘G_DY’][‘db’][‘persistent’] )@mysql_close($this->conn);
              }
              }

              • 谁能帮我吗 1

                能帮我改改吗?
                exec($sql) )return array();
                if( ! mysql_num_rows($result) )return array();
                $rows = array();
                while($rows[] = mysql_fetch_array($result,MYSQL_ASSOC)){}
                mysql_free_result($result);
                array_pop($rows);
                return $rows;
                }

                public function newinsertid()
                {
                return mysql_insert_id($this->conn);
                }

                public function showtables($tables)
                {
                return mysql_num_rows(mysql_query(“show tables like ‘%”.$tables.”%'”,$this->conn));
                }

                public function setlimit($sql, $limit)
                {
                return $sql. ” LIMIT {$limit}”;
                }

                public function exec($sql)
                {
                $this->arrSql[] = $sql;
                if( $result = mysql_query($sql, $this->conn) ){
                return $result;
                }else{
                if(mysql_error()!=”){
                syError(“{$sql}执行错误: ” . mysql_error());
                }else{
                return TRUE;
                }
                }
                }

                public function affected_rows()
                {
                return mysql_affected_rows($this->conn);
                }

                public function getTable($tbl_name)
                {
                return $this->getArray(“DESCRIBE {$tbl_name}”);
                }

                public function __construct($dbConfig)
                {
                $linkfunction = ( TRUE == $dbConfig[‘persistent’] ) ? ‘mysql_pconnect’ : ‘mysql_connect’;
                $this->conn = $linkfunction($dbConfig[‘host’].”:”.$dbConfig[‘port’], $dbConfig[‘login’], $dbConfig[‘password’]);
                if(!$this->conn || !mysql_select_db($dbConfig[‘database’], $this->conn)){
                echo ‘数据库无法链接,如果您是第一次使用,请先执行安装程序网润科技建站程序 m.cqbaidu.com‘;exit;
                }
                $this->exec(“SET NAMES UTF8”);
                if($this->version() > ‘5.0.1’) {
                $this->exec(“set sql_mode=””);
                }
                }
                public function version() {
                return mysql_get_server_info($this->conn);
                }

                public function __val_escape($value) {
                return ‘\”.$value.’\”;
                }

                public function __destruct()
                {
                if( TRUE != $GLOBALS[‘G_DY’][‘db’][‘persistent’] )@mysql_close($this->conn);
                }
                }

                • 消失的853 0

                  作者您好,如果是mysql_pconnect的话该怎么改呢

                    • 默北 默北 6

                      @消失的853 在主机前面加上 p:
                      如 $mysqli = new mysqli(‘p:‘"localhost", "user", "password", "database");

                        • 谁能帮我吗 1

                          @默北 大哥能帮我看下吗 我的好像跟你写的不一样不知道怎么改

                          • lvxiongmao 0

                            @默北 大哥帮我看看
                            exec($sql) )return array();
                            if( ! mysql_num_rows($result) )return array();
                            $rows = array();
                            while($rows[] = mysql_fetch_array($result,MYSQL_ASSOC)){}
                            mysql_free_result($result);
                            array_pop($rows);
                            return $rows;
                            }

                            public function newinsertid()
                            {
                            return mysql_insert_id($this->conn);
                            }

                            public function showtables($tables)
                            {
                            return mysql_num_rows(mysql_query(“show tables like ‘%”.$tables.”%'”,$this->conn));
                            }

                            public function setlimit($sql, $limit)
                            {
                            return $sql. ” LIMIT {$limit}”;
                            }

                            public function exec($sql)
                            {
                            $this->arrSql[] = $sql;
                            if( $result = mysql_query($sql, $this->conn) ){
                            return $result;
                            }else{
                            if(mysql_error()!=”){
                            syError(“{$sql}执行错误: ” . mysql_error());
                            }else{
                            return TRUE;
                            }
                            }
                            }

                            public function affected_rows()
                            {
                            return mysql_affected_rows($this->conn);
                            }

                            public function getTable($tbl_name)
                            {
                            return $this->getArray(“DESCRIBE {$tbl_name}”);
                            }

                            public function __construct($dbConfig)
                            {
                            $linkfunction = ( TRUE == $dbConfig[‘persistent’] ) ? ‘mysql_pconnect’ : ‘mysql_connect’;
                            $this->conn = $linkfunction($dbConfig[‘host’].”:”.$dbConfig[‘port’], $dbConfig[‘login’], $dbConfig[‘password’]);
                            if(!$this->conn || !mysql_select_db($dbConfig[‘database’], $this->conn)){
                            echo ‘数据库无法链接,如果您是第一次使用,请先执行安装程序网润科技建站程序 m.cqbaidu.com‘;exit;
                            }
                            $this->exec(“SET NAMES UTF8”);
                            if($this->version() > ‘5.0.1’) {
                            $this->exec(“set sql_mode=””);
                            }
                            }
                            public function version() {
                            return mysql_get_server_info($this->conn);
                            }

                            public function __val_escape($value) {
                            return ‘\”.$value.’\”;
                            }

                            public function __destruct()
                            {
                            if( TRUE != $GLOBALS[‘G_DY’][‘db’][‘persistent’] )@mysql_close($this->conn);
                            }
                            }

                        • 再见帅人智 9

                          取消mysql的用户 怎么理解呢?

                          • 再见帅人智 9

                            搞了一下午才看到你的高见。求教: public function __construct($hostname, $username, $password, $database) {
                            if (!$this->link = mysql_connect($hostname, $username, $password)) {
                            trigger_error(‘Error: Could not make a database link using ‘ . $username . ‘@’ . $hostname);
                            }
                            怎么改呢?

                              • 默北 默北 6

                                @再见帅人智 $mysqli = new mysqli("localhost", "user", "password", "database");
                                if ($mysqli->connect_errno) {
                                echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
                                }

                              • itfanr 0

                                感谢

                                • Hety 9

                                  用PDO更好,性能好又安全。PDO是趋势

                                  • sphinxer 9

                                    确实。 抱着老掉牙的东西咋进步

                                  • 来自外部的引用: 2

                                    • 解决Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: – Thecy
                                    • Apache web submit 403 error, access forbbiden, Undefined variable - Web前端 - 阿里欧歌