php - "Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource"

标签 php mysql database

我收到此错误:

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource

这是我的 Connection.php :

$userDB_server = "";
$userDB_user = "";
$userDB_password = "";
$userDB_database = "";
$connection = mysql_connect("$userDB_server","$userDB_user","$userDB_password") or die ("Unable to establish a DB connection");
$userDB = mysql_select_db("$userDB_database", $connection) or die ("Unable to establish a DB connection");


$gameDB_server = "";
$gameDB_user = "";
$gameDB_password = "";
$gameDB_database = "";
$gameDB_connection = mysql_connect("$gameDB_server","$gameDB_user","$gameDB_password", true) or die ("Unable to establish a DB connection");
$gameDB = mysql_select_db("$gameDB_database", $gameDB_connection) or die ("Unable to establish a DB connection");

这是我的功能:

require_once('Connection.php');
$findQuery = sprintf("SELECT * FROM `Keys` WHERE `ID` = '$gID'");
$findResult = mysql_query($findQuery, $connection) or die(mysql_error());
$resultRow = mysql_fetch_assoc($findResult) or die(mysql_error());

错误出现在“$findResult = mysql_query($findQuery, $connection) or die(mysql_error());” 但我没有看到任何问题。

我尝试过的:

  • 我尝试过在第二个连接上使用或不使用“true”, 似乎没有什么区别。
  • 回显 $connection 和 $gameDB_connection 没有显示任何内容,
  • 在 $connection 上使用 var_dump 显示“资源(9) 类型(mysql 链接)”
  • 从 mysql_query 中删除 $connection 使其连接到 其他数据库(gameDB_connection),我收到一个错误,该表 不存在(不在该数据库上)。
  • 从查询中添加/更改/删除反引号(`)似乎 对错误没有影响
  • 变量 $gID 回显正确,因此它不为空(它的 1001 在 本例)
  • 如果我以实际的 sql 形式运行 SELECT 部分(而不是通过 php), 它正确地列出了它们
  • Connection.php 在其他地方使用(一页显示 同时从两个数据库)成功。其他地方没有错误

有人知道出了什么问题吗?

最佳答案

根据评论,听起来问题是由使用 require_once() 引起的在函数内部。

两件事之一正在发生。要么:

  1. 您已经包含 Connection.php在其他地方,所以当您到达该函数时,它实际上并未包含在内..因为 require_onceonce 部分.

    或者...

  2. 第一次调用该函数时,它可以正常工作,但第二次调用该函数时,该文件已被包含,并且不会再次包含。

问题是,当第一次包含该文件时(假设该文件来自此函数),$connection变量是在函数作用域中创建的,并且与任何其他函数变量一样,在函数结束时消失。当您第二次调用该函数时,不会发生包含,因为您正在使用 require_once .

您可以通过调用 require() 来解决此问题而不是require_once() ,但是每次调用该函数时最终都会重新连接到数据库 - 这是很多不必要的开销。将 include 移到函数外部,然后将连接传递到函数中,或者将其用作全局变量,这样会更简洁。

看起来像这样:

require_once('Connection.php');

function getResult() {
    global $connection;

    $findQuery = "SELECT * FROM `Keys` WHERE `ID` = '$gID'";
    $findResult = mysql_query($findQuery, $connection) or die(mysql_error());
    $resultRow = mysql_fetch_assoc($findResult) or die(mysql_error());
} 

综上所述,这段代码有两个主要问题。

  1. 您正在使用mysql_*已弃用并将很快从 PHP 新版本中删除的函数。有关更多详细信息,请参阅此问题:Why shouldn't I use mysql_* functions in PHP?

    切换到像mysqli_*这样的东西实际上并不难。而是使用函数 - 有一组非对象函数,它们与您现在使用的函数几乎相同。

  2. 您在查询中包含了一个变量,但没有正确转义它。至少你应该调用mysql_real_escape_string() (或 mysqli_real_escape_string() ),但更好的解决方案是查看准备好的语句。您可以在此处找到有关准备好的语句的更多信息:How can I prevent SQL injection in PHP?

关于php - "Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17386035/

相关文章:

php - Paypal 沙盒和 Omnipay

mysql - 在 Perl 中工作 MySQL 查询失败

mysql - 河马和长耳兔的关系

php - 将多维数组简化为线性数组

php - 无法在 Windows xampp 中使用命令行导入任何 sql 文件

MySQL查询恢复

SQL查询以查找同一列中值的乘积

PHP 7 和 phpMyAdmin 的 PHP fatal error

php - 检查英国日期是否在 SQL 中的两个日期之间

php - 在 Laravel 中,有没有办法删除旧的已撤销/过期的护照 token