mysql_fetch_array()和mysql_query()问题

时间:2013-03-27 02:34:36

标签: php mysql wordpress buddypress

我在错误日志中不断收到此错误,但我不确定原因。我到处寻找并谷歌了,但我迷路了,我需要帮助:

[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在代码中被进一步声明。一旦我解决了这个问题,它就完美无缺。感谢所有回答!

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您不需要将$conn放在mysql_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);

答案 2 :(得分:0)

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

$getlatlong = mysql_query("SELECT * FROM zipcode WHERE zip='". $url[2] ."'");