0

My error: Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in .../gallery.php on line 81

I'm sure a little thing, but here's my code. It should grab the image etc from each row, if approved, and display it accordingly.

Doesn't seem to want to play nice :(

Has anyone experience this issue before?

<?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'; ?>
Dave
  • 691
  • 2
  • 11
  • 25

4 Answers4

2

you are refilling the $result with $result = mysql_fetch_array($result); so it is obvious that $result is no longer a mysql resource. later in your code you are trying to fetch data from it with mysql_fetch_object($result). that should be the issue. try assigning to different variable names.

San
  • 542
  • 6
  • 21
0

If mysql_query() does not return a resource, it means that the query has failed. This is most likely due to an SQL error of some sort.

The quotes around count also should be backticks:

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

Since you overwrite $result after running the query, you cannot access the query result set further down in your code.

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

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

I'm not sure if the first $result actually contains the content you are looking for, but there does not appear to be another query.

Arjan
  • 9,784
  • 1
  • 31
  • 41
0

If mysql_query returned 0 rows, mysql_fetch_array() returns False

mysql_fetch_array()

Returns an array of strings that corresponds to the fetched row, or FALSE if there are no more rows.


$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);
    ...
    ...
}
Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
0

You have overwrite the value of $result, make changes into your code like below:

$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

Now use this variable $result_query here

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

Now you will not get the error

pkachhia
  • 1,876
  • 1
  • 19
  • 30