Mysqli 从 table 错误中获取值

Mysqli getting values from table error

我将我的网站连接到数据库并且连接成功,但是当我尝试从数据库中的 table 获取值时出现此错误:

Warning: mysql_query(): Access denied for user ''@'localhost' (using password: NO) in /home/username/public_html/root/connecti.php on line 17

Warning: mysql_query(): A link to the server could not be established in /home/username/public_html/root/connecti.php on line 17

连接成功

第 17 行:

$sql = mysql_query("SELECT * FROM 'tblusers' LIMIT 0, 30");

查看您的 SELECT 查询,您会发现用单引号引用 table 名称是问题所在,因为数据库引擎将其视为字符串文字而不是数据库对象。

您的意思是使用 backtique 对其进行转义

"SELECT * FROM `tblusers` LIMIT 0, 30"

我有根据的猜测是您使用 mysqli_connect() but then attempt to use mysql_query() 建立了连接。根据文档:

mixed mysql_query ( string $query [, resource $link_identifier = NULL ] )

link_identifier

The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() had been called with no arguments. If no connection is found or established, an E_WARNING level error is generated.

如果您查看 mysql_connect() 的手册页,您会发现它可能正在尝试使用您尚未设置的系统范围的凭据。

您不能根据自己的喜好混合使用数据库扩展。停止使用旧版扩展并坚持使用 mysqli。