mysql_fetch_object():提供的参数不是有效的MySQL结果资源?

时间:2012-10-26 05:15:18

标签: php mysql

我的错误:Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in .../gallery.php on line 81

我确定一件小事,但这是我的代码。如果获得批准,它应该从每一行抓取图像等,并相应地显示它。

似乎不想玩得很好:(

之前是否有人遇到此问题?

<?php
$page = 'gallery';
include 'header.php';

$can_vote = TRUE; //expand

$query = "SELECT COUNT(`id`) as 'count' FROM `$db_name`.`gallerytable` WHERE `approve` = 1 LIMIT 1;";
$result = mysql_query($query);
$result = mysql_fetch_array($result);
$total_entry = $result['count'];

$current_page = (int) $_GET['page'];
$current_page = ($current_page == 0) ? 1 : $current_page;

?>

<header id="main-head" class="clearfix">
    <div style="height:103px;overflow:hidden;">&nbsp;</div>
    <nav id="main-nav">
    </nav><!-- #main-nav -->
</header><!-- #main-head -->

<div id="main-content" class="clearfix">

    <?php if($total_entry > 20): ?>
    <?php
    $start_range = range(1, $total_entry, 20);
    $end_range = ($total_entry < 40) ? array(20, $total_entry) : range(20, $total_entry, 20);
    ?>

    <div id="paging-wrap" class="clearfix">
        <ul id="paging-nav">
        <?php
        $output = '';

        //prev link
        $prev_page = $current_page - 20;
        if($prev_page >= 1)
            $output .= '<li><a href="?p=gallery&amp;page='.$prev_page.'">&lt;</a></li>';

        //show page links
        foreach($start_range as $key => $number) {
            $output .= '<li><a href="?p=gallery&amp;page='.$number.'">'.$number.'-'.$end_range[$key]."</a></li>\n";
            $last_start_number = $number;
        }

        //hack last link
        $output = str_replace('-</a></li>', '-'.$total_entry.'</a></li>', $output);
        if($last_start_number == $total_entry) {
            $output = str_replace('<li><a href="?p=gallery&amp;page='.$last_start_number.'">'.$last_start_number.'-'.$last_start_number.'</a></li>','<li><a href="?p=gallery&amp;page='.$last_start_number.'">'.$last_start_number.'</a></li>', $output );
        }
        //set active page
        $output = str_replace('<a href="?p=gallery&amp;page='.$current_page.'">', '<a href="?p=gallery&amp;page='.$current_page.'" class="active">', $output);

        $next_page = $current_page + 20;
        if($next_page <= $last_start_number)
            $output .= '<li><a href="?p=gallery&amp;page='.$next_page.'">&gt;</a></li>';

        echo $output;
        ?>
        </ul><!-- #paging-nav -->
        <span id="paging-title">Display</span>
    </div><!-- #paging-wrap -->
    <?php else: ?>
    <div id="blank-paging"></div>
    <?php endif; //total entry > 20 ?>
    <div id="gallery-wrap">
    <?php 
    if($total_entry > 0) :
        //show entry
        $ip_address = $_SERVER['REMOTE_ADDR'];
        $limit_start = $current_page - 1;
        $number = $current_page;

        $url = ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on' ) ? 'https://' : 'http://';
        $url .= $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
        ?>
            <?php while($row = mysql_fetch_object($result)): ?>
                <div class="photo-wrap can-vote">
                    <img height="113px" style="height:113px;overflow:hidden;line-height:0;" src="<?php echo $url ?>/inc/timthumb.php?src=<?php echo $url ?>/photos/<?php echo $row->photo ?>&amp;w=136&amp;h=113&amp;q=100&amp;a=t" alt="photo-contest" />
                </div>
            <?php $number++; endwhile; ?>
    <?php else: ?>
    <h2>no-one here</h2>
    <?php endif ?>
    </div><!-- #gallery-wrap -->
</div><!-- #main-content -->
<footer id="footer-main">

</footer><!-- #footer-main -->
<?php
$form_key = $formKey->generateKey();
$_SESSION['form_key'] = $form_key;
?>
<script> var key_val = '<?php echo $form_key ?>';</script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/jquery-ui.min.js"></script>
<?php include 'footer.php'; ?>

4 个答案:

答案 0 :(得分:2)

您正在使用$result = mysql_fetch_array($result);重新填充$ result,因此很明显$ result不再是mysql资源。稍后在您的代码中,您尝试使用mysql_fetch_object($result)从中获取数据。这应该是问题。尝试分配不同的变量名称。

答案 1 :(得分:0)

如果mysql_query()未返回资源,则表示查询失败。这很可能是由于某种SQL错误造成的。

count周围的引号也应该是反引号:

$query = "SELECT COUNT(`id`) as `count` FROM `$db_name`.`gallerytable` WHERE `approve` = 1 LIMIT 1;";

由于您在运行查询后覆盖$result,因此无法在代码中进一步访问查询结果集。

$result = mysql_query($query);
$result = mysql_fetch_array($result); // <--- You overwrite $result here

<?php while($row = mysql_fetch_object($result)): ?>

我不确定第一个$result是否确实包含您要查找的内容,但似乎没有其他查询。

答案 2 :(得分:0)

如果 mysql_query 返回0行, mysql_fetch_array()返回False

<强> mysql_fetch_array()

  

返回与获取的行对应的字符串数组,或   如果没有更多行,则为FALSE。


$query = "SELECT COUNT(`id`) as 'count' FROM `$db_name`.`gallerytable` WHERE `approve` = 1 LIMIT 1;";
$result = mysql_query($query);
$numrows = mysql_num_rows($result);

if($numrows > 0){
    $result = mysql_fetch_array($result);
    ...
    ...
}

答案 3 :(得分:0)

您已覆盖 $ result 的值,请对您的代码进行更改,如下所示:

$query = "SELECT COUNT(`id`) as 'count' FROM `$db_name`.`gallerytable` WHERE `approve` = 1 LIMIT 1;";
$result_query = mysql_query($query); //<--- see here I have changed $result with $result_qury
$result = mysql_fetch_array($result_query);//<--- see here I have changed $result with $result_qury

现在在这里使用此变量 $ result_query

<?php while($row = mysql_fetch_object($result_query)): ?>

现在你不会收到错误