所以我有3个DB表,除了表的名称外,各方面都相同(数据不同)。我做到了这一点,所以我可以使用一个像这样的开关的代码:
function disp_bestof($atts) {
extract(shortcode_atts(array(
'topic' => ''
), $atts));
$connect = mysql_connect("localhost","foo","bar");
if (!$connect) { die('Could not connect: ' . mysql_error()); }
switch ($topic) {
case "attorneys":
$bestof_query = "SELECT * FROM attorneys p JOIN (awards a, categories c, awardLevels l) ON (a.id = p.id AND c.id = a.category AND l.id = a.level) ORDER BY a.category, a.level ASC";
$category_query = "SELECT * FROM categories";
$db = mysql_select_db('roanoke_BestOf_TopAttorneys');
$query = mysql_query($bestof_query);
$categoryQuery = mysql_query($category_query);
break;
case "physicians":
$bestof_query = "SELECT * FROM physicians p JOIN (awards a, categories c, awardLevels l) ON (a.id = p.id AND c.id = a.category AND l.id = a.level) ORDER BY a.category, a.level ASC";
$category_query = "SELECT * FROM categories";
$db = mysql_select_db('roanoke_BestOf_TopDocs');
$query = mysql_query($bestof_query);
$categoryQuery = mysql_query($category_query);
break;
case "dining":
$bestof_query = "SELECT * FROM restaurants p JOIN (awards a, categories c, awardLevels l) ON (a.id = p.id AND c.id = a.category AND l.id = a.level) ORDER BY a.category, a.level ASC";
$category_query = "SELECT * FROM categories";
$db = mysql_select_db('roanoke_BestOf_DiningAwards');
$query = mysql_query($bestof_query);
$categoryQuery = mysql_query($category_query);
break;
default:
$bestof_query = "switch on $best did not match required case(s)";
break;
}
$category = '';
while( $result = mysql_fetch_array($query) ) {
if( $result['category'] != $category ) {
$category = $result['category'];
//echo "<div class\"category\">";
$bestof_content .= "<h2>".$category."</h2>\n";
//echo "<ul>";
现在,对于前两种情况,这一切都完美无缺,但第三种“就餐”打破了这个错误:
警告:mysql_fetch_assoc():提供的参数不是有效的MySQL结果资源...在第78行
第78行是底部的while()
。我已检查并仔细检查,无法确定问题所在。这是'餐馆'的数据库结构:
CREATE TABLE `restaurants` (
`id` int(10) NOT NULL auto_increment,
`restaurant` varchar(255) default NULL,
`address1` varchar(255) default NULL,
`address2` varchar(255) default NULL,
`city` varchar(255) default NULL,
`state` varchar(255) default NULL,
`zip` double default NULL,
`phone` double default NULL,
`URI` varchar(255) default NULL,
`neighborhood` varchar(255) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=249 DEFAULT CHARSET=utf8
有谁看到我在这里做错了什么?我正在将“用餐”传递给该功能,正如我之前所说,开关中的前两种情况正常。
我确定这是愚蠢的......
答案 0 :(得分:1)
查看您的查询代码 - 运行$bestof_query
,无论它是否已设置为有效的SQL。我的第一个猜测是,你在某处错过了'dining'
并得到了默认情况。
另外,仔细检查您的数据库名称是否正确(它们相当复杂)并且所有数据库都具有相同的权限。你在检查$db
是否属实?
答案 1 :(得分:1)
您在该查询中收到sql错误。你应该回应你的mysql错误并检查它来修复你的查询。你得到的警告是因为你将boolean false传递给mysql_fetch_assoc(),它正在期待一个结果集。如果有错误,mysql_query()返回false。
答案 2 :(得分:1)
您应该始终将您使用的变量初始化为某个(null)值,然后在使用之前检查它。我的猜测是你的第三种情况(dining
)永远不会因为一些拼写错误的标识符而被执行。这会导致default:
运行,之后无论如何都会执行while()
。但是,$query
未设置为有用的任何内容。
因此,您应该在default:
处理程序中抛出异常或以其他方式中断执行。或者,您可以在$query = null;
之前初始化switch()
,并在while()
时仅执行$query !== null
循环。
在相关的说明中:当您改为使用以下内容时,您可能会更有效地编写代码(请注意异常处理程序):
$db_name = null;
$table = null;
switch ($topic) {
case "attorneys":
$db_name = 'roanoke_BestOf_TopAttorneys';
$table = 'attorneys'
break;
case "physicians":
$db_name = 'roanoke_BestOf_TopDocs';
$table = 'physicians'
break;
case "dining":
$db_name = 'roanoke_BestOf_DiningAwards';
$table = 'restaurants'
break;
default:
throw new Exception("Unknown topic.");
break;
}
$bestof_query = "SELECT * FROM $table p JOIN (awards a, categories c, awardLevels l) ON (a.id = p.id AND c.id = a.category AND l.id = a.level) ORDER BY a.category, a.level ASC";
$category_query = "SELECT * FROM categories";
$db = mysql_select_db($db_name);
$query = mysql_query($bestof_query);
$categoryQuery = mysql_query($category_query);