0

I am having really difficult time to find the mistake on my following code. It seems all the code is correct but I am having this warning error every time I try to run the page. Does anyone has a good knowledge on how to solve this problem? Yes, I have checked the connection, which is fine and the table called event is there with event_id in the database server. Thanks in advance.

The code generates records from the database and separates it according to the page number. I have set 1 record per page.

Warning: mysql_result() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xyz/xyz/xyz/event.php on line 89

<?php
$link = mysql_connect('localhost', '#', '#');
if (!$link) {
    die('Could not connect: ' . mysql_error());
                            }
$per_page = 1; 

$pages_query = mysql_query("SELECT count(event_id) FROM event ORDER BY event_date"); 
$pages = ceil(mysql_result($pages_query, 0) / $per_page); 
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1; 
$start = ($page - 1) * $per_page; 

$query = mysql_query("SELECT * FROM event LIMIT $start, $per_page");
                        while ($query_row = mysql_fetch_assoc($query)) {
                                    $event_name = $query_row['event_name'];
                                    $event_info = $query_row['event_info'];
?>   

Line 89 is

$pages = ceil(mysql_result($pages_query, 0) / $per_page); 
DirtyBit
  • 16,151
  • 4
  • 28
  • 54
Sandesh Rana
  • 97
  • 4
  • 13
  • 1
    In the name of God, Please stop using the _deprecated_ mysql functions – DirtyBit Sep 24 '15 at 22:02
  • 1
    You're getting this error because your query has failed. You're assuming that it works, but `mysql_query` is returning a boolean false, which you're passing directly into `mysql_result`. Check to see what the return value is, and use `mysql_error` to see what the problem actually is. – andrewsi Sep 24 '15 at 22:05

3 Answers3

1

First:

Every single function that starts with mysql_ instead of mysqli_ is deprecated.

That means that you should not use them. That means that your scripts WILL BREAK HORRIBLY when those functions are removed, which will happen very soon.

It has been a very, very long time since those functions have been considered worst practice. There is no excuse to ever use those functions today.

Second:

mysql_query() returns a boolean false when the query fails.

Your real problem is in the line above. Figure out why $pages_query = mysql_query("SELECT count(event_id) FROM event ORDER BY event_date"); is failing, and your script will work.

Third:

Switch to the mysqli_ functions today.

Ghedipunk
  • 1,229
  • 10
  • 22
  • Please don't recommend `mysqli` to newcomers. They'll never use parameterization (because that's way too tedious). And judging from the questions we get afterwards, the resulting code will universally look worse. – mario Sep 24 '15 at 22:13
  • @Ghedipunk, thanks for the advice – Sandesh Rana Sep 24 '15 at 22:18
1

You forgot to do use mysql_select_db($database_name) to select the database after mysql_connect().

Thus, the mysql_query() failed and returned false, and hence the mysql_result() received a boolean instead of a result-set-resource, which in turn produced the message you see.


That said, the comments about deprecation of the mysql extension (in favor of mysqli or PDO) are very valid.

Alex Shesterov
  • 24,284
  • 11
  • 75
  • 95
0

Let's keep it deprecated for the sake of understanding, Try this:

<?php
$host = "localhost";
$username = "your_username_here";
$password = "your_pass_here";
$dbname = "your_db_name_here";

$con = mysql_connect($host, $username, $password) or die ("Could not connect to Server");
$db = mysql_select_db($dbname, $con) or die ("Could not connect to database: $dbname");

$per_page = 1; 

$pages_query = mysql_query("SELECT count(event_id) FROM event ORDER BY event_date"); 
$pages = ceil(mysql_result($pages_query, 0) / $per_page); 
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1; 
$start = ($page - 1) * $per_page; 

$query = mysql_query("SELECT * FROM event LIMIT $start, $per_page");

      while ($query_row = mysql_fetch_assoc($query)) {
            $event_name = $query_row['event_name'];
            $event_info = $query_row['event_info'];
      }
?>

Notice: If you've understood this well, I urge you to move to either mysqli or PDO.

DirtyBit
  • 16,151
  • 4
  • 28
  • 54