2013-02-06 76 views
0

我想从数据库执行一些数据过滤。过滤后,会话变量丢失,我得到这个错误:丢失会话数据

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in ..\test.php on line 34.

有人能帮忙吗?

FORM:

<form action= "test.php" method='get'> 
    <select name="Type"> 
      <option value="winter" name="winter">winter</option> 
    </select> 

    <input type='submit' value = 'filter'> 
</form> 

CODE:

<?php 
session_start(); 
$_SESSION['winter'] = $_GET['Type']; 
$type = $_SESSION['winter'] ; 

include "mysqlConnect.php"; 
    //check for a page number. If not, set it to page 1 
    if (!(isset($_GET['pagenum']))){ 
    $pagenum = 1; 
    }else{ 
    $pagenum = $_GET['pagenum']; 
    } 
    //query for record count to setup pagination 

    $data = mysql_query("SELECT * FROM tblPhotos WHERE Type='$type' "); 
    $rows = mysql_num_rows($data); 
    //number of photos per page 
    $page_rows = 16; 
    //get the last page number 
    $last = ceil($rows/$page_rows); 
    //make sure the page number isn't below one, or more than last page num 
    if ($pagenum < 1){ 
    $pagenum = 1; 
    }elseif ($pagenum > $last){ 
    $pagenum = $last; 
    } 
    //Set the range to display in query 
    $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows; 
    //get all of the photos 

    $dynamicList = ""; 
    $sql = mysql_query("SELECT * FROM tblPhotos WHERE Type='$type' $max "); 
    //check for photos 
     $photoCount = mysql_num_rows($sql); //LINE 34 
    echo $photoCount; 
    if ($photoCount > 0){ 
    while($row = mysql_fetch_array($sql)){ 
     $photoID = $row["PhotoID"]; 
     $photoName = $row["photoName"]; 
     $category = $row["category"]; 
     $dynamicList .= ' 
          <div class="thumb"> 
            <a href="photo.php?id=' . $photoID . '"><img class="clip" src="galleryPhotos/' . $photoID . '.jpg" alt="' . $photoName . '" width="175" border="0" /></a> 
          </div> 
         '; 
     } 
    }else{ 
    $dynamicList = "There are no photos at this time!"; 
     } 

    mysql_close(); 

     echo '<p style="text-align:center; font-weight:bold;">Page ' . $pagenum . ' of ' . $last . '</p>'; 
     if ($pagenum == 1){ 
     echo '<div class="pagination" align="center"><ul>'; 
     }else{ 
      echo '<div class="pagination" align="center"><ul><li><a href="' . $_SERVER['PHP_SELF'] . '?pagenum=1">« first</a></li>'; 
     $previous = $pagenum-1; 
    } 
     //check if number of pages is higher than 1 
     if($last != 1){ 
     //Loop from 1 to last page to create page number links 
     for($i = 1; $i <= $last; $i++){ 
      echo '<li><a href="' . $_SERVER['PHP_SELF'] .'?pagenum=' . $i . '">' . $i . '</a></li>'; 
     } 
    } 
    if ($pagenum == $last){ 
     echo '</div>'; 
    }else{ 
     $next = $pagenum+1; 
     echo '<li><a href="' . $_SERVER['PHP_SELF'] . '?pagenum=' . $last . '">last »</a></li></ul></div>'; 

} 
    echo $dynamicList; 
?> 
+0

[**请不要在新代码中使用'mysql_ *'函数**](http://bit.ly/phpmsql)。他们不再被维护[并被正式弃用](https://wiki.php.net/rfc/mysql_deprecation)。看到[**红框**](http://j.mp/Te9zIL)?学习[*准备的语句*](http://j.mp/T9hLWi),并使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [这篇文章](http://j.mp/QEx8IB)将帮助你决定哪个。如果你选择PDO, [这里是一个很好的教程](http://www.brightmeup.info/article.php?a_id=2)。 –

回答

0
Your Code: mysql_query("SELECT * FROM tblPhotos WHERE Type='$type' $max "); 

阅读错误信息,它告诉你什么是错与查询。查看了查询。我倾向于同意。