关于php:警告:mysql_select_db():提供的参数不是有效的MySQL-Link资源

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

这是我不断出错的代码。 相同的包含文件在其他页面上也可以使用,但我在此页面上只会遇到问题。 这是错误

警告:mysql_select_db():提供的参数在第18行的/var/www/html/spywgc/adm/ctshell/getproduct/getproduct.php中不是有效的MySQL-Link资源

这是实际的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php require_once('../../../Connections/spyware_adm.php'); ?>
<?php require_once('../../../includes/lib_gen.php'); ?>

<?php
//function for returing status of website
function Visit($url)
{
    echo $url;
    mysql_select_db($database_spyware, $spyware) || die(mysql_error());
    $select_url="select product_id from sp_url where url like '%{$url}%'";
    echo $select_url;
    $run_url= mysql_query($select_url, $spyware);
    $result_descr = mysql_fetch_assoc($run_url);
    echo $result_descr;
    return $result_descr;

}
?>


我认为这两个变量$spyware$database_spywarenull,因为它们是在函数范围之外定义的(如果有的话),而不是声明为全局的。 尝试添加

1
global $database_spyware, $spyware

在函数Visit(..)的开头。


mysql_select_db()期望第二个参数是资源标识符=您的连接。 问题是,您正在将此功能作为函数运行,在该函数内部未建立连接。 您必须以如下方式启动您的功能:

1
2
3
4
5
6
function Visit($url)
{
    $spyware = mysql_connect(); // set this to connect properly
    echo $url;
    mysql_select_db($database_spyware, $spyware) || die(mysql_error());
    // the rest of your function goes on ...


您是否检查了初始数据库连接调用是否成功? 像大多数mysql函数一样,失败时返回false。 同样,您还没有检查查询是否成功。 最有可能失败并返回FALSE,然后将其传递给fetch调用:

1
2
3
4
5
6
7
8
9
10
11
12
$database_spyware = mysql_connect(...);
if ($database_spyware === FALSE) {
   die("Connection failed:" . mysql_error());
}

... etc ...

$run_query = mysql_query(...);

if ($run_query === FALSE) {
   die("Query failed:" . mysql_error());
}

应该是可接受的最低限度的错误处理。