Note that there are some explanatory texts on larger screens.

plurals
  1. POPagination doesn't work
    text
    copied!<pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;PHP test&lt;/title&gt; &lt;link href="style.css" rel="stylesheet" type="text/css"&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="formDiv"&gt; &lt;form action="testphp.php" method="post"&gt; Email: &lt;input type="text" name="email"&gt;&lt;br&gt; &lt;textarea name="comment" rows="10" cols="40"&gt;Your comment here.&lt;/textarea&gt; &lt;input type="submit" value="submit"&gt; &lt;/form&gt; &lt;div id="show"&gt; &lt;?php $con=mysqli_connect("","root","","my_db"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $result = mysqli_query($con,"SELECT * FROM Comments"); echo "&lt;table border='1'&gt; &lt;tr&gt; &lt;th&gt;Email&lt;/th&gt; &lt;th&gt;Comment&lt;/th&gt; &lt;/tr&gt;"; while($row = mysqli_fetch_array($result)) { echo "&lt;tr&gt;"; echo "&lt;td&gt;" . $row['Email'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['comment'] . "&lt;/td&gt;"; echo "&lt;/tr&gt;"; } echo "&lt;/table&gt;"; mysqli_close($con); ?&gt; &lt;/div&gt; &lt;/div&gt; &lt;?php $con=mysqli_connect("","root","","my_db"); if($_SERVER["REQUEST_METHOD"] == "POST") { if(empty($_POST["comment"])) {echo "You have to write something. &lt;br&gt;"; echo "&lt;meta http-equiv=\"refresh\" content=\"2;URL=testphp.php\"&gt;"; } else { if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } if(empty($_POST["email"])) { $sql="INSERT INTO Comments (Email, comment) VALUES ('$_POST[email]', '$_POST[comment]')"; //echo "1 record added."; echo "&lt;meta http-equiv=\"refresh\" content=\"2;URL=testphp.php\"&gt;"; if (!mysqli_query($con,$sql)) { die('Error: ' . mysqli_error($con)); } } else{ if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$_POST["email"])) { echo "Invalid email format &lt;br&gt;"; echo "&lt;meta http-equiv=\"refresh\" content=\"2;URL=testphp.php\"&gt;"; } else{ $sql="INSERT INTO Comments (Email, comment) VALUES ('$_POST[email]', '$_POST[comment]')"; echo "&lt;meta http-equiv=\"refresh\" content=\"2;URL=testphp.php\"&gt;"; if (!mysqli_query($con,$sql)) { die('Error: ' . mysqli_error($con)); } } } } } $db = mysql_selectdb('my_db', $con) or trigger_error("SQL", E_USER_ERROR); //number of rows in table $sql = "SELECT COUNT (*) FROM Comments"; $result = mysql_query($sql, $con) or trigger_error("SQL", E_USER_ERROR); $r = mysql_fetch_row($result); $numRows = $r[0]; //number of rows to show per page $rowsPerPage = 10; $totalPages = ceil($numRows/$rowsPerPage); //get current page or set default if(isset($_GET['currentpage']) &amp;&amp; is_numeric($_GET['currentpage'])){ $currentpage = (int) $_GET['currentpage']; } else{ $currentpage = 1; } if($currentpage &gt; $totalPages){ $currentpage = $totalPages; } if($currentpage &lt; 1){ $currentpage = 1; } $offset = ($currentpage - 1) * $rowsPerPage; //get info from DB $sql = "SELECT comment FROM Comments LIMIT $offset, $rowsPerPage"; $result = mysql_query($sql, $con) or trigger_error("SQL", E_USER_ERROR); while($list = mysql_fetch_assoc($result)){ echo $list['comment']; echo "&lt;br /&gt;"; } //build pagination links if($currentpage &gt; 1){ echo " &lt;a href='{$_SERVER['PHP_SELF']}?currentpage=1'&gt;&lt;&lt;&lt;/a&gt; "; $prevpage = $currentpage - 1; echo " &lt;a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'&gt;&lt;&lt;/a&gt; "; } $range = 3; for ($x = ($currentpage - $range); $x &lt; (($currentpage + $range) + 1); $x++) { // if it's a valid page number... if (($x &gt; 0) &amp;&amp; ($x &lt;= $totalpages)) { // if we're on current page... if ($x == $currentpage) { // 'highlight' it but don't make a link echo " [&lt;b&gt;$x&lt;/b&gt;] "; // if not current page... } else{ echo " &lt;a href='{$_SERVER['PHP_SELF']}?currentpage=$x'&gt;$x&lt;/a&gt; "; } } } if($currentpage != $totalpages){ $nextpage = $currentpage + 1; echo " &lt;a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'&gt;&gt;&lt;/a&gt; "; echo " &lt;a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'&gt;&gt;&gt;&lt;/a&gt; "; } ?&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>That's my code, I got the pagination code from another site and tried to change it to suit my needs but I can't make it work. I get this warning for this line of code </p> <pre><code>$db = mysql_selectdb('my_db', $con) or trigger_error("SQL", E_USER_ERROR);) Warning: mysql_selectdb() expects parameter 2 to be resource. </code></pre> <p>This is my first time programming in php and I've tried every bit of code on pagination but it just doesn't work...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload