0

我在错误日志中不断收到此错误,但我不知道为什么。我到处寻找并用谷歌搜索,但我迷路了,我需要帮助:

[Wed Mar 27 02:14:56 2013] [error] PHP Warning:  mysql_query() expects parameter 2 to
 be resource, null given in /nas/wp/www/cluster-1434/XXXXXXXX/wp-content/themes/mytheme
/groups/single/home.php on line 23
[Wed Mar 27 02:14:56 2013] [error] PHP Warning:  mysql_fetch_array() expects 
 parameter  
1 to be resource, null given in /nas/wp/www/cluster-1434/XXXXXXXX/wp-content/themes
/mytheme/groups/single/home.php on line 24

这是它引用的代码:

$getlatlong = mysql_query("SELECT * FROM zipcity WHERE zip='".$url[2]."'",$con);
$ll = mysql_fetch_array($getlatlong);

如果您需要,这是完整的代码:

<?php
global $bp;
global $current_user;
$group_name = $bp->groups->current_group->name;
$group_id = $bp->groups->current_group->id;
$groupll = groups_get_groupmeta( $group_id, $meta_key = 'latlong');
$groupnamecount = strlen($group_name);
$groupcity = groups_get_groupmeta( $group_id, $meta_key = 'city');
$groupstate = groups_get_groupmeta( $group_id, $meta_key = 'state');
$mapaddress = $group_name.'+'.$groupcity.'+'.$groupstate;
if ($groupnamecount == '5')
{
$grouptype = "zip"; 
}
if ($grouptype == "zip") {

$url = explode('/',$_SERVER['REQUEST_URI']);
$zip = $url[2]; 
$getlatlong = mysql_query("SELECT * FROM zipcode WHERE zip='".$url[2]."'",$con);
$ll = mysql_fetch_array($getlatlong);
} else {
$groupcity = groups_get_groupmeta( $group_id, $meta_key = 'city');
$groupstate = groups_get_groupmeta( $group_id, $meta_key = 'state');
$mapaddress = $group_name.'+'.$groupcity.'+'.$groupstate;
}

更新

问题是 $con 在代码的下方被声明了。一旦我解决了这个问题,它就完美地工作了。感谢所有回答!

4

3 回答 3

0

看起来您的$con变量要么从未定义,要么连接失败。如果您根本没有指定连接参数,PHP 将自动使用最后一个 MySQL 连接,如下所示:

$getlatlong = mysql_query("SELECT * FROM zipcode WHERE zip='". $url[2] ."'");
于 2013-03-27T02:41:36.013 回答
0

您不需要将$conninmysql_query()函数放入它只是为了执行您的 SQL 语句,因为您已经这样做了,使用mysql_select_db() 您的$con设置连接到数据库,如下所示..

  $getlatlong = mysql_query("SELECT * FROM zipcity WHERE zip='".$url[2]."'",$con);
    $ll = mysql_fetch_array($getlatlong);

首先你的连接应该像这样..

$conn = mysql_connect($host,$user,$pass);  // mysql config
mysql_select_db('databasename',$conn);    // connection to database

然后像这样查询..

$getlatlong = mysql_query("SELECT * FROM zipcity WHERE zip='".$url[2]."'");
        $ll = mysql_fetch_array($getlatlong);
于 2013-03-27T02:38:31.443 回答
0

您的连接 $con 有问题,它没有连接并返回 null。

于 2013-03-27T02:37:00.517 回答