首先我这样做:
$zip_code_array = mysql_query("SELECT * FROM zip_code WHERE (lon BETWEEN '$lng_min_rnd' AND '$lng_max_rnd') AND (lat BETWEEN '$lat_min_rnd' and '$lat_max_rnd')") or die (mysql_error());
while($zip_code_cells = mysql_fetch_array($zip_code_array))
{
$zip_codes_raw = $zip_code_cells['zip_code'];
$zip_codes_in_distance .= $zip_codes_raw.", ";
}
这可以工作并吐出像这样的EX:07110,07111,07112等的邮政编码: 但后来我这样做了:
$user_list_array = mysql_query("SELECT * FROM member_search WHERE IN($zip_codes_in_distance) AND gender = '$gender_of_interest_session'");
while($user_list = mysql_fetch_array($user_list_array))
{
$id_from_array = $user_list['id'];
$username_from_array = $user_list['user_name'];
$defaultpic_from_array = $user_list['defaultpic'];
$city_from_array = $user_list['city'];
}
我在
上获得了Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
$user_list_array = mysql_query("SELECT * FROM member_search WHERE IN($zip_codes_in_distance) AND gender = '$gender_of_interest_session'");
我似乎无法找出查询它出现的zipcodes列表的正确方法。当我将邮政编码结果转换为字符串并将所有邮政编码用逗号分隔时,我确实认为这与某些事情有关。
答案 0 :(得分:2)
您的sql语句有错误。 $zip_codes_in_distance
的结尾为,
,导致失败。
应该是这样的。
$zips = array();
$zip_code_array = mysql_query("SELECT * FROM zip_code WHERE (lon BETWEEN '$lng_min_rnd' AND '$lng_max_rnd') AND (lat BETWEEN '$lat_min_rnd' and '$lat_max_rnd')") or die (mysql_error());
while($zip_code_cells = mysql_fetch_array($zip_code_array))
{
$zip_codes_raw = $zip_code_cells['zip_code'];
$zips[]= $zip_codes_raw;
}
$zip_codes_in_distance = "'". implode("','" , $zips). "'";
答案 1 :(得分:1)
首先,检查查询是否成功:
<?php
$result = mysql_query("SELECT something FROM foo WHERE bar = 'foo'");
if( $result === false )
{ # Query not a succes :(
echo 'We cannot execute the query, the error: '.mysql_error(); // Remove mysql_error() when you put the site online
}
else
{ # the query was a succes
if( mysql_num_rows() <= 0 )
{ # If there are 0 or less rows
echo 'We cannot find a result';
}
else
{ # You get 1 or more rows
// Use fetch_assoc, because this is faster then fetch_array and you don't need a fetch_array
while( $row = mysql_fetch_assoc($result) )
{
echo $row['something'];
}
}
}
一些资源:
答案 2 :(得分:0)
修正了它:而不是像这样拆分数组:
$zip_codes_in_distance .= $zip_codes_raw.", ";
我是这样做的:
$zip_codes_in_distance .= "'".$zip_codes_raw."', ";
然后对于MySQL IN运算符我就像这样做了:
IN($zip_codes_in_distance '0')
我添加了零,没有,(逗号),因为数组在每个邮政编码的末尾添加一个逗号。当它这样做时,它在字符串的末尾留下了额外的逗号,从而产生了SQL错误。 0只是将字符串放在字符串的末尾,与搜索结果无关。