这是我的问题。我想查询三个表——状态、点和状态点。
状态包含stateid和名称。点:点ID、点LAT、点LNG。State\\u points:Stateid,Pointid。
我需要构建一个数组来显示状态,然后显示相应的点。要做到这一点,我认为最好的方法是像本教程那样:http://www.bitsofphp.com/avoid-executing-mysql-queries-within-loops/
查询所有状态,使用StateID作为ArrayID创建一个数组
然后从points and state\\u points表中选择lat、lng和stateid。使用在第二次查询中选择的stateid作为阵列id,将点(lat和lng)添加到阵列中
global $wpdb;
$states = $wpdb->query("SELECT * from wp_states");
echo $states;
while ($state = mysql_fetch_array($states)) {
$arrayState[$state[\'stateid\']]=$state;
}
$points = $wpdb->query("SELECT wp_state_points.statepointid, wp_state_points.stateid, wp_state_points.pointid, wp_points.pointid, wp_points.lat, wp_points.lng FROM wp_state_points, wp_points WHERE wp_state_points.pointid = wp_points.pointid");
while ($point = mysql_fetch_array($points)) {
$arrayState[$point[\'stateid\']][\'points\'][]=$point;
}
foreach ($arrayState as $astate) {
echo $astate[\'name\'];
foreach ($astate[\'points\'] as $apoint) {
echo $apoint[\'lat\'];
}
}
但我收到类似这样的消息“警告:mysql\\u fetch\\u array():提供的参数在中不是有效的mysql结果资源…”我知道我的查询正在返回数据(至少是第一个数据)。我认为wordpress查询根本不支持这一点。
我可以使用get\\u results返回数组。但要实现这一点,ArrayID必须与StateID相同
我能做些什么来实现这一目标?
谢谢