“警告:mysql_query():提供的参数不是有效的MySQL-Link资源” [英] "Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource"

查看:362
本文介绍了“警告:mysql_query():提供的参数不是有效的MySQL-Link资源”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误:

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)或die(mysql_error ));
但是我在任何地方都看不到问题。

The error is on "$findResult = mysql_query($findQuery, $connection) or die(mysql_error());" But I don't see a problem anywhere.

我尝试过的


  • 我试过在第二个连接上没有真,
    在任何地方似乎没有什么区别。

  • 回显$连接和$ gameDB_connection显示没有,

  • 在$ connection上使用var_dump显示resource(9)of type(mysql link)

  • 从mysql_query中删除$连接连接到
    其他DB(gameDB_connection),我得到一个错误,表
    不存在(它不在该数据库)。

  • 添加/更改/从查询中删除反引号(`)似乎
    对错误没有影响

  • 变量$ gID echo正确,因此它不为null $ b this case)

  • 如果我在实际的sql形式(而不是通过php)运行SELECT部分​​,
    它会正确列出它们

  • Connection.php用于其他地方(一个页面同时从两个数据库读取
    )。

  • I've tried with and without the "true" on the second connection, didn't seem to make a difference anywhere.
  • Echoing the $connection and $gameDB_connection shows nothing,
  • Using var_dump on $connection shows "resource(9) of type (mysql link)"
  • Removing the $connection from the mysql_query has it connect to the other DB (gameDB_connection) and I get an error that the table doesn't exist (its not on that DB).
  • Adding / changing / removing the backquote ( ` ) from the query seems to have no effect on the error
  • The variable $gID echo's correctly, so it's not null (its 1001 in this case)
  • If I run the SELECT part in the actual sql form (instead of via php), it lists them all correctly
  • The Connection.php is used in other places (one page reads from both databases at the same time) successfully. No errors anywhere else

任何人都知道有什么问题?

Anyone have any idea what's wrong?

推荐答案

基于这些意见,听起来问题是由在函数中使用 require_once()引起的。

Based on the comments, it sounds like the problem is caused by using require_once() inside a function.

两件事之一正在发生。

One of two thing is happening. Either:


  1. 您已包含 Connection.php ,所以当你到达函数时,它不会实际包含..因为 once 部分 require_once

  1. You've already included Connection.php somewhere else, so when you get to the function, it's not actually included.. because of the once part of require_once.

或...

是第一次调用函数时工作,

It is working the first time you call the function, but the second time you call it, the file has already been included and does not get included again.

问题是,在第二次调用该文件时,当文件首次被包含时(假设是来自这个函数),在函数作用域中创建 $ connection 变量,像任何其他函数变量一样,结束函数。当你第二次调用函数时,include不会发生,因为你使用 require_once

The problem is that when the file is first included (assuming that's from within this function), the $connection variable is created in the function scope, and like any other function variable, is gone at the end of the function. When you call the function a second time, the include doesn't happen because you're using require_once.

你可以通过调用 require()而不是 require_once()来解决这个问题,但是最终会重新连接到数据库每次调用函数 - 这是很多不必要的开销。只需要移动函数外部的include就可以了,或者把连接传递给函数,或者把它作为一个全局变量使用。

You could probably fix this by calling require() instead of require_once(), but that will end up reconnecting to the database every time you call the function - which is a lot of unnecessary overhead. It's much cleaner to just move the include outside of the function, and either pass the connection into the function, or use it as a global variable.

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());
} 

所有这一切,这个代码有两个主要问题。

All that being said, there's 2 major problems with this code.


  1. 您正在使用已弃用的 mysql _ * 函数,从新版本的PHP中删除。有关详情,请参阅此问题:为什么我不应该使用mysql_ *函数在PHP中?

  1. You're using the mysql_* functions which are deprecated and will soon be removed from new versions of PHP. See this question for more details: Why shouldn't I use mysql_* functions in PHP?

实际上并不难切换到类似 mysqli _ *

It's not actually that hard to switch to something like the mysqli_* functions instead - there's a non-object set of functions that are almost identical to what you're using now.

您在查询中包含一个未正确使用的变数逃脱它。至少应该调用 mysql_real_escape_string()(或 mysqli_real_escape_string()),但更好的解决方案是看看准备好的语句。您可以在此处找到有关准备语句的详细信息:如何防止SQL注入PHP?

You're including a variable in your query without properly escaping it. At the very least you should be calling mysql_real_escape_string() (or mysqli_real_escape_string()), but a better solution is to look into prepared statements. You can find more information on prepared statements here: How to prevent SQL injection in PHP?

这篇关于“警告:mysql_query():提供的参数不是有效的MySQL-Link资源”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆