0

I am designing a website, which allows users to register and login - Upon logging in, the user is presented with additional links (only visible to registered users). One of these links includes, "My Bookmarks" - The "My Bookmarks" (index.php) code is as follows:

My Bookmarks (index.php) Code:

<?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");
}
?>

Sorry for the formatting issues, I still haven't completely figured out how to copy and paste code effectively.

Anyways, my question is derived from the two errors I receive, which are:

Error One:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/kethcart/public_html/440/finalproject/htdocs/bookmark/index.php on line 50

Error Two:

Warning: mysql_free_result() expects parameter 1 to be resource, boolean given in /home/kethcart/public_html/440/finalproject/htdocs/bookmark/index.php on line 58

I understand there are more than enough posted questions on this topic, but I can't seem to apply them to my situation. If someone could provide a suggestion on how to fix this or even a link to another question or solution, that would be great!

I appreciate everyone's help and patience - I don't know what I'd do without this community.

Thanks,

Rockmandew

rockmandew
  • 792
  • 11
  • 20
  • Lol, so in trying to suppress error you get errors. Can I ask, have you tried just simply `mysql_fetch_array(mysql_query($query), ...)`? in other words, without the `@` – SpYk3HH Nov 21 '13 at 19:36
  • Johannes - I saw this exact post, however, I proceeded to ask my question because I was unsure of how to apply the solution to my code. Not that I'm looking for someone to do the code for me or state the explicit correction, I'm just looking for some guidance as to how to apply the correction. Thanks. – rockmandew Nov 21 '13 at 19:37
  • SpYk3HH - I have not tried that method, I will test/apply your suggestion momentarily and get back to you with the results. – rockmandew Nov 21 '13 at 19:38
  • Also, you do realize that `mysql_fetch_array` only gets one row, right? Shouldn't you want something like: `While ($row = mysql_fetch_array` – SpYk3HH Nov 21 '13 at 19:38
  • Stop with mysql_*() functions now and move to PDO or mysqli_*(). – AbraCadaver Nov 21 '13 at 19:38
  • AbraCadaver - I would love to use another method, however, since this is an academic assignment, I am required to follow the instructions. SpYk3HH - I was unaware that it only returns one row. I will play around with the suggested code. Thank you. – rockmandew Nov 21 '13 at 19:40
  • Another thing to note, you can use `$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());` to get more info and to make sure `$result` is a result, else it will die and you will see the error in browser – SpYk3HH Nov 21 '13 at 19:41
  • You said this was a school thing or whatever. Your best resource, the PHP API, see [mysql_fatch_array here](http://us2.php.net/mysql_fetch_array) and keep up with that sight for any other PHP function – SpYk3HH Nov 21 '13 at 19:43
  • @AbraCadaver That doesn't really matter unless he means to move to a PHP 5.4+ server. Quite frankly, I myself will not update till they get all of the "kinks" out and move on. I still use the old way because I've had 100x less head aches with it. Until some of the issues i've reported and have set to follow on the php site, i'm not upgrading my code and wouldn't suggest anyone else too either. "If it's not broke, don't fix it!" – SpYk3HH Nov 21 '13 at 19:57
  • The `mysql_*` functions are **no longer maintained** and shouldn't be used in any new codebase. It is being phased out in favor of newer APIs. Instead you should use [**prepared statements**](https://www.youtube.com/watch?v=nLinqtCfhKY) with either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). – tereško Nov 21 '13 at 22:29

1 Answers1

1

Quick answer. You're getting the errors because something is wrong with your query. When using mysql_query, it's important to note, that if the query fails, you get a BOOLEAN return of FALSE. Thus why you're being told ...expects parameter 1 to be resource, boolean given...

To avoid such issue's, try using a die statement to get "feedback" of what might be wrong. Something like:

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

See also:

SpYk3HH
  • 21,961
  • 10
  • 67
  • 81
  • I think this answer will solve your problem @rockmandew . try this and update us... – CyberBoy Nov 21 '13 at 19:57
  • @MohdSuleman I think the answer check means it worked ... lol – SpYk3HH Nov 21 '13 at 21:11
  • @Spy3HH , answer was not checked, when i wrote this comment. :P i was curious that is why i wrote this comment, i knew it would work , that is why i Up voted it... – CyberBoy Nov 21 '13 at 21:13
  • @MohdSuleman Oh i'm sure it wasn't when you commented, that's why i commented, to let you know and poke fun. lol! Have a great weekend! – SpYk3HH Nov 21 '13 at 21:58
  • :) oh that was fun... i had added you in my circles on google plus... – CyberBoy Nov 21 '13 at 22:01