php中mysql_connect()连接报错
在使用mysql_connect()函数连接数据库时遇到错误信息:
warning: mysql_connect(): mysql extension is deprecated and will be removed in the future: use mysqli or pdo instead in /var/www/html/connect.php on line 9
错误原因:
已弃用mysql_extension扩展,因为它存在安全隐患和性能问题。建议使用mysqli或pdo扩展替代。
立即学习“PHP免费学习笔记(深入)”;
解决方案:
使用mysqli或pdo:
使用mysqli_connect()函数替代mysql_connect():
$con = mysqli_connect("localhost", "root", "", "database");
使用pdo类建立连接:
$dsn = "mysql:host=localhost;dbname=database"; $username = "root"; $password = ""; try { $con = new PDO($dsn, $username, $password); } catch (PDOException $e) { echo $e->getMessage(); }
以上就是PHP连接数据库报错:mysql_connect()函数已弃用,如何解决?的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号