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

凉白开 PHP18127,196103字数 881阅读2分56秒阅读模式

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报错文章源自运维生存时间-https://www.ttlsa.com/php/deprecated-mysql-connect/

display_errors = On
改为
display_errors = Off

鉴于这个服务器都是给用户用的,有时候他们需要报错(...都是给朋友用的,^_^),不能这做,让他们改程序吧,看方案2.文章源自运维生存时间-https://www.ttlsa.com/php/deprecated-mysql-connect/

解决方法2:

常用的php语法连接mysql如下文章源自运维生存时间-https://www.ttlsa.com/php/deprecated-mysql-connect/

<?php
$link = mysql_connect('localhost', 'user', 'password');
mysql_select_db('dbname', $link);

改成mysqi
<?php
$link = mysqli_connect('localhost', 'user', 'password', 'dbname');

常用mysql建表SQL如下文章源自运维生存时间-https://www.ttlsa.com/php/deprecated-mysql-connect/

<?php
//  老的
mysql_query('CREATE TEMPORARY TABLE `table`', $link);
// 新的
mysqli_query($link, 'CREATE TEMPORARY TABLE `table`');

解决方法三:

在php程序代码里面设置报警级别文章源自运维生存时间-https://www.ttlsa.com/php/deprecated-mysql-connect/

<?php
error_reporting(E_ALL ^ E_DEPRECATED);

Deprecated的问题就这样解决掉了,不过还是建议大家尽快取消mysql的用法,全部都走向mysqli或者mysqlnd等等。mysql确实是太不安全而且太老旧了。文章源自运维生存时间-https://www.ttlsa.com/php/deprecated-mysql-connect/

文章源自运维生存时间-https://www.ttlsa.com/php/deprecated-mysql-connect/文章源自运维生存时间-https://www.ttlsa.com/php/deprecated-mysql-connect/
weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
凉白开
  • 本文由 发表于 22/08/2013 11:06:27
  • 转载请务必保留本文链接:https://www.ttlsa.com/php/deprecated-mysql-connect/
  • Deprecated
  • extension
  • mysql_connect
评论  18  访客  16
    • 追梦小窝
      追梦小窝 0

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

      • ET
        ET 0

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

        请问这个怎么改

        • 绝缘材料
          绝缘材料 1

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

          • lvxiongmao
            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
                  消失的853 0

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

                    • 默北
                      默北 6

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

                        • 谁能帮我吗
                          谁能帮我吗 1

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

                          • lvxiongmao
                            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
                                itfanr 0

                                感谢

                                • Hety
                                  Hety 9

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

                                  • sphinxer
                                    sphinxer 9

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

                                  评论已关闭!