简体   繁体   English

PHPUnit未定义函数mysql_connect()

[英]PHPUnit undefined function mysql_connect()

When I try to connect to my MySQL database from PHP code all seems to be fine, all queries are executed successfully. 当我尝试从PHP代码连接到我的MySQL数据库时,一切似乎都很好,所有查询均成功执行。 However, when the code is tested with unit tests in PHPUnit, I get the following fatal PHP error: 但是,在PHPUnit中使用单元测试对代码进行测试时,出现以下致命PHP错误:

PHP Fatal error: Call to undefined function mysql_connect(). PHP致命错误:调用未定义函数mysql_connect()。

As indicated, this code works fine outside of unit tests, but PHPUnit generates errors. 如前所述,这段代码在单元测试之外运行良好,但是PHPUnit会产生错误。

It's likely you're using a namespace, and therefore mysql_query or MySQLi is undefined there. 您可能正在使用名称空间,因此mysql_queryMySQLi在此处未定义。

Try the following: 请尝试以下操作:

$mysqli = new \MySQLi(...);

I believe that you're testing with a version of PHP 7 or higher. 我相信您正在使用PHP 7或更高版本进行测试。

In the php documentation mysql_query , they describe the following php文档mysql_query中 ,它们描述了以下内容

Warning This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. 警告此扩展在PHP 5.5.0中已弃用,在PHP 7.0.0中已被删除。 Instead, the MySQLi or PDO_MySQL extension should be used. 相反,应使用MySQLi或PDO_MySQL扩展。 See also MySQL: choosing an API guide and related FAQ for more information. 另请参见MySQL:选择API指南和相关的FAQ,以获取更多信息。 Alternatives to this function include: mysqli_query() PDO::query() 该函数的替代品包括:mysqli_query()PDO :: query()

So, two possible solutions: 因此,有两种可能的解决方案:

  1. Downgrade PHP to a lower version (< 7); 将PHP降级到较低版本(<7);
  2. Replace your mysql functions with MySQLi or PDO. 用MySQLi或PDO替换mysql函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM