mysql_fetch_array()& mysql_free_result()错误

时间:2013-11-21 19:31:16

标签: php mysql

我正在设计一个允许用户注册和登录的网站 - 登录后,会向用户显示其他链接(仅对注册用户可见)。其中一个链接包括“我的书签” - “我的书签”(index.php)代码如下:

我的书签(index.php)代码:

<?php
  session_start();
    //check session first
  if (!isset($_SESSION['email'])){
    echo "You are not logged in!";
      exit();
  }
  else{
    //include the header
  include ("../includes/header.php");
     require_once ('../../mysql_connect.php');
    echo ("<center>"); 
    echo ("<h3>Bookmark</h3><p>");
    echo ("<a href=add.php>Add a record</a><p>"); 
    echo ("<a href=searchform.php>Search records</a><p>"); 
    //Set the number of records to display per page
    $display = 5;
    //Check if the number of required pages has been determined
  if(isset($_GET['p'])&&is_numeric($_GET['p'])){//Already been determined
    $pages = $_GET['p'];
  }
  else{//Need to determine
    //Count the number of records;
    $query = "SELECT COUNT(id) FROM bookmark";
    $result = @mysql_query($query); 
    $row = @mysql_fetch_array($result, MYSQL_NUM);
    $records = $row[0]; //get the number of records
    //Calculate the number of pages ...
    if($records > $display){//More than 1 page is needed
        $pages = ceil($records/$display);
    }else{
        $pages = 1;
    }
}// End of p IF.

//Determine where in the database to start returning results ...
if(isset($_GET['s'])&&is_numeric($_GET['s'])){
    $start = $_GET['s'];
}else{
    $start = 0;
}

//Make the paginated query;
$query = "SELECT * FROM bookmark LIMIT $start, $display"; 
$result = @mysql_query ($query);

//Table header:
echo "<table cellpadding=5 cellspacing=5 border=1><tr>
<th>Title</th><th>Comment</th><th>URL</th><th>*</th><th>*</th></tr>"; 

//Fetch and print all the records...
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo "<tr><td>".$row['title']."</td>"; 
    echo "<td>".$row['comment']."</td>"; 
    echo "<td><a href=".$row['url']." target=_blank>".$row['url']."</a></td>"; 
    echo "<td><a href=deleteconfirm.php?id=".$row['id'].">Delete</a></td>"; 
    echo "<td><a href=updateform.php?id=".$row['id'].">Update</a></td></tr>"; 
} // End of While statement
echo "</table>"; 
mysql_free_result ($result); // Free up the resources.         
mysql_close(); // Close the database connection.

//Make the links to other pages if necessary.
if($pages>1){
    echo '<br/><table><tr>';
    //Determine what page the script is on:
    $current_page = ($start/$display) + 1;
    //If it is not the first page, make a Previous button:
    if($current_page != 1){
        echo '<td><a href="index.php?s='. ($start - $display) . '&p=' . $pages. '"> Previous </a></td>';
    }
    //Make all the numbered pages:
    for($i = 1; $i <= $pages; $i++){
        if($i != $current_page){ // if not the current pages, generates links to that page
            echo '<td><a href="index.php?s='. (($display*($i-1))). '&p=' . $pages .'"> ' . $i . ' </a></td>';
        }else{ // if current page, print the page number
            echo '<td>'. $i. '</td>';
        }
    } //End of FOR loop
    //If it is not the last page, make a Next button:
    if($current_page != $pages){
        echo '<td><a href="index.php?s=' .($start + $display). '&p='. $pages. '"> Next </a></td>';
    }

    echo '</tr></table>';  //Close the table.
}//End of pages links
//include the footer
include ("../includes/footer.php");
}
?>

很抱歉格式化问题,我还没有完全弄清楚如何有效地复制和粘贴代码。

无论如何,我的问题来自我收到的两个错误:

错误一:

  

警告:mysql_fetch_array()要求参数1为资源,布尔值在第50行的/home/kethcart/public_html/440/finalproject/htdocs/bookmark/index.php中给出

错误二:

  

警告:mysql_free_result()要求参数1为资源,布尔值在第58行的/home/kethcart/public_html/440/finalproject/htdocs/bookmark/index.php中给出

据我所知,关于这个主题的帖子已经发布了很多,但我似乎无法将它们应用到我的情况中。如果有人可以提供关于如何解决此问题的建议,甚至提供其他问题或解决方案的链接,那就太棒了!

我感谢大家的帮助和耐心 - 我不知道如果没有这个社区,我会做些什么。

谢谢,

Rockmandew

1 个答案:

答案 0 :(得分:1)

快速回答。您收到错误是因为您的查询出了问题。使用mysql_query时,请务必注意,如果查询失败,您将获得BOOLEAN FALSE次返回。这就是为什么你被告知...... expects parameter 1 to be resource, boolean given...

要避免此类问题,请尝试使用die语句获取可能出错的“反馈”。类似的东西:

$query = "SELECT COUNT(id) FROM bookmark";
$result = mysql_query($query) or die ("Error in query: $query. <br />".mysql_error());

另见: