Pass variable in page which has pagination

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
kostas
Forum Newbie
Posts: 1
Joined: Wed Aug 31, 2011 9:06 am

Pass variable in page which has pagination

Post by kostas »

Hi,

I am having a page (epipla.php) which is displaying some data (names of companies & their information) and when a user clicks on a company name, he gets redirected in another page (meta.php). On the redirection the epipla.php passes the company id via a link which contains the id (example : <a href="meta.php?id={$row['id']}"></a>).
The meta.php displays the company's information and then the comments added for the certain company.
The comments are displayed using pagination. And here comes the problem. The first page works just fine displaying all the 4 first comments but when i click on the second or any other next page to see the rest comments i got the error :

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\epipla\meta.php on line 36

The pages' code is given below:

epipla.php :

<?php

ini_set ('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE);

require ('header.html');

?>

<div style="margin-left:850px; margin-top:0px:">
<a href="login1.php" style="color:black;">Login</a> | <a href="register1.php" style="color:black;">Register</a>
</div>

<br /><div style="margin-left : 300px; margin-top : 25px;">
(Keyword: company name)
<form action="search.php" method="post">
<input type="text" name="search" size="40" />
<input type="submit" name="submit" class="s_button" value="Search" />
</form></div><br />

<?php

$dbhost = "localhost";
$dbname = "companies";
$dbuser = "root";
$dbpass = "";

//Connect to database

mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());
mysql_query("SET NAMES 'utf8' COLLATE 'utf8_unicode_ci'");

if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * 4;
$sql = "SELECT * FROM c_information ORDER BY id ASC LIMIT $start_from, 4";
$rs_result = mysql_query ($sql);

print '<div style=" margin-left:80px; margin-top:25px;"><table style=" border-style:solid;border-color:#98bf21; border-radius: 1em; width:800px;"><tr><td>';
while ($row = mysql_fetch_assoc($rs_result)) {

print "<div style=\"margin-left : 25px; margin-right : 100px;\">";
print "<table border=\"0\">
<tr height=\"50\">
<td>
<p align=\"left\"><br /><a href=\"meta.php?id={$row['id']}\">" . $row['title'] . "</a></p>
</td>
</tr>
<tr>
<td>
<table border=\"0\" align=\"center\">
<tr>
<td><img src=\"" . $row['image'] . "\" width=\"150\" height=\"80\" /></td>
<td><div style=\" margin-left:150px;\">
<table>
<tr>
<td width=\"30\" height=\"25\" align=\"center\"><img src=\"email.png\"/></td>
<td>email : " . $row['email'] . "</td>
</tr>
<tr>
<td width=\"30\" height=\"25\" align=\"center\"><img src=\"telephone.png\"/></td>
<td>" . $row['phone'] . "</td>
</tr>
<tr>
<td width=\"30\" height=\"25\" align=\"center\"><img src=\"website.png\"/></td>
<td><a href=\"" . $row['web_address'] . "\">website</a></td>
</tr>
</table><br/></div>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>" .$row['address'] . "</td>
</tr>
</table><br/><hr/><br/>";
print "</div>";
}
print "</td></tr></table></div>";

$sql = "SELECT COUNT(id) FROM c_information";
$rs_result = mysql_query($sql);
$row = mysql_fetch_row($rs_result);
$total_records = $row[0];
$total_pages = ceil($total_records / 4);

print "<br /><div style=\"margin-left : 80px;\">Pages : ";

for ($i=1; $i<=$total_pages; $i++) {
print "<a href='epipla.php?page=".$i."'>".$i."</a> ";
}
print "</div>";
?>

<?php
print "<br /><br /><br />";
require ('footer.html');
?>


meta.php:

<?php

ini_set ('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE);

require ('header.html');

?>

<div style="margin-left:850px; margin-top:0px:">
<a href="login1.php" style="color:black;">Login</a> | <a href="register1.php" style="color:black;">Register</a>
</div>

<br /><div style="margin-left : 300px; margin-top : 25px;">
(Keyword: company name)
<form action="search.php" method="post">
<input type="text" name="search" size="40" />
<input type="submit" name="submit" class="s_button" value="Search" />
</form></div><br />

<?php

$dbhost = "localhost";
$dbname = "companies";
$dbuser = "root";
$dbpass = "";
$mid = $_GET['id'];

//Connect to database

mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());
mysql_query("SET NAMES 'utf8' COLLATE 'utf8_unicode_ci'");

$query = mysql_query("SELECT * FROM c_information WHERE id=$mid");
$row = mysql_fetch_array( $query );
$id = $row['id'];
print '<div style=" margin-left:80px; margin-top:25px;"><table style=" border-style:solid;border-color:#98bf21; border-radius: 1em; width:800px;"><tr><td>';
print "<div style=\"margin-left : 25px; margin-right : 100px;\">";
print "<table border=\"0\">
<tr height=\"50\">
<td>
<p align=\"left\"><br />" . $row['title'] . "</p>
</td>
</tr>
<tr>
<td>
<table border=\"0\" align=\"center\">
<tr>
<td><img src=\"" . $row['image'] . "\" width=\"150\" height=\"80\" /></td>
<td><div style=\" margin-left:150px;\">
<table>
<tr>
<td width=\"30\" height=\"25\" align=\"center\"><img src=\"email.png\"/></td>
<td>email : " . $row['email'] . "</td>
</tr>
<tr>
<td width=\"30\" height=\"25\" align=\"center\"><img src=\"telephone.png\"/></td>
<td>" . $row['phone'] . "</td>
</tr>
<tr>
<td width=\"30\" height=\"25\" align=\"center\"><img src=\"website.png\"/></td>
<td><a href=\"" . $row['web_address'] . "\">website</a></td>
</tr>
</table><br/></div>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>" .$row['address'] . "</td>
</tr>
</table><br/><br/>";
print "</div>";
print "</td></tr></table></div>";





$que_ry = "SELECT AVG(rating) FROM karekla WHERE id=$id";
$result = mysql_query($que_ry);
while($values = mysql_fetch_array($result)){

print "<p>The average rating is " . $values['AVG(rating)'] . "</p>";
}

print "<br /><h4>Comments:</h4><br />";

if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * 4;
$sql = "SELECT * FROM karekla WHERE id=$id ORDER BY company_id DESC LIMIT $start_from, 4";
$rs_result = mysql_query ($sql);

while ($r = mysql_fetch_assoc($rs_result)) {
print "<p>" . $r['entry'] . "</p>";
print "<p>Posted : " . $r['date_entered'] . "</p>";
print "<p>" . $r['rating'] . "</p><hr />";
}

$sqlq = "SELECT COUNT(company_id) FROM karekla WHERE id=$id ";
$rs_resultq = mysql_query($sqlq);
$rowq = mysql_fetch_row($rs_resultq);
$total_records = $rowq[0];
$total_pages = ceil($total_records / 4);

print "<br />Pages : ";

for ($i=1; $i<=$total_pages; $i++) {
print "<a href='meta.php?page=".$i."'>".$i."</a> ";
}


?>

<?php
print "<br /><br /><br />";
require ('footer.html');
?>

Any help would be appreciated,
Thanks in advance!
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Re: Pass variable in page which has pagination

Post by ok »

The PHP manual says:
mysql_query() will also fail and return FALSE if the user does not have permission to access the table(s) referenced by the query.
SO it seems that you don't have permissions to access the table 'c_information'.
Post Reply