gpt4 book ai didi

mysql_fetch_array() 问题

转载 作者:行者123 更新时间:2023-11-29 05:45:43 31 4
gpt4 key购买 nike

所以我有 3 个数据库表,除了表的名称外,它们在各个方面都是相同的(数据不同)。我这样做是为了可以像这样使用带有开关的一段代码:

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

有没有人看到我在这里做错了什么?我将“用餐”传递给函数,正如我之前所说,开关中的前两种情况工作正常。

我确定这是愚蠢的事情......

最佳答案

您应该始终将您使用的变量初始化为某个(空)值,然后在使用前检查它。我的猜测是您的第三个案例 (dining) 永远不会因为某些拼写错误的标识符或其他原因而被执行。这会导致 default: 运行,之后您的 while() 将执行。但是,$query 没有设置任何有用的值。

因此,您应该在 default: 处理程序中抛出异常或以其他方式中断执行。或者,您可以在 switch() 之前初始化 $query = null; 并且只在 $query 时执行 while() 循环!== 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);

关于mysql_fetch_array() 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2404554/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com