0

我的错误: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

4 回答 4

2

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

于 2012-10-26T05:26:31.137 回答
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真的包含您要查找的内容,但似乎没有另一个查询。

于 2012-10-26T05:18:51.933 回答
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);
    ...
    ...
}
于 2012-10-26T05:20:09.393 回答
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)): ?>

现在你不会得到错误

于 2012-10-26T05:37:34.917 回答