Click here to Skip to main content
15,902,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my problem that I want a pager to show 10 record in page and I found this great code at:



Host = "localhost"
user="root"
password=""(Nothing)
 
<?php
    $con = mysql_connect("localhost","root","") or die("Unable to connect");
    $db = mysql_select_db("test",$con) or die("Unable to select DB");
 
    if(isset($_GET['page']))
    {
        $page = $_GET['page'];
    }
    else
    {
        $page = 1;
    }
 
    $sf = ($page-1) * 10;
    $sql = "SELECT * FROM students LIMIT ".$sf.",10";
    $rs = mysql_query($sql,$con);
    //echo $rs;
?>
<html>
    <head>
        <title>Pages</title>
    </head>
    <body>
        <?php
            $sql1 = "SELECT COUNT(name) FROM students";
            $rs1 = mysql_query($sql1,$con);
            $row1 = mysql_fetch_row($rs1);
            $total = $row1[0];
            $tp = ceil($total/10);
 
            for($i = 1; $i <= $tp; $i++)
            {
                echo "<a href='test.php?page=".$i."'>".$i."</a> ";
            }
        ?>
        <table>
            <tr>
                <th>Name</th>
                <th>Phone Number</th>
            </tr>
            <?php
                while($row = mysql_fetch_assoc($rs))
                {
            ?>
            <tr>
                <td><?php echo $row['name']; ?></td>
                <td><?php echo $row['ph_no']; ?></td>
            </tr>
            <?php
                }
            ?>
        </table>
        <?php
            $sql1 = "SELECT COUNT(name) FROM students";
            $rs1 = mysql_query($sql1,$con);
            $row1 = mysql_fetch_row($rs1);
            $total = $row1[0];
            $tp = ceil($total/10);
 
            for($i = 1; $i <= $tp; $i++)
            {
                echo "<a href='test.php?page=".$i."'>".$i."</a> ";
            }
        ?>
    </body>
</html>


when I retrieve table data without condition like in the code("SELECT * FROM students LIMIT ".$sf.",10";) it work but when I add an condition like ("SELECT * FROM students where Instructor-Email=". $_SESSION['ID'] ." OR Instructor-Email IS NULL LIMIT".$sf.",10") it doesn't work I was doubt my query syntax so I download query builder program (flyspeed sql query) and it built the query as:"Select * From students Where (Instructor-Email = '. $_SESSION['ID'] .') Or (Instructor-Email Is Null) LIMIT 0,10" but still didn't work
can you tell me what is the problem? with all my thanks for your efforts
Posted
Updated 13-Oct-13 10:22am
v3
Comments

1 solution

I'm too tired to compare your code with some that I know works, so I'll have to leave that to you right now.

Disclaimer:
(1) you shouldn't use mysql_*** functions, they've been deprecated - use PDO or mySqli instead. (PDO is more versatile)
(2) plenty of hard-coded stuff here - avoid like the plague in your own production code.
(3) no error checking done

sample-deleteMe.php
PHP
<!doctype html>
<head>
<style>
.heading
{
	color: green;
	font-weight: bold;
}
</style>
</head>
<body>
<?php
	function createDatabase($dbName)
	{
		$sql = "create database ".$dbName;
		mysql_query($sql);
		mysql_select_db($dbName);
		$sql = "CREATE TABLE IF NOT EXISTS `test` (`id` int(11) NOT NULL,`text` varchar(10) NOT NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;";
		mysql_query($sql);
		
		for ($i=0; $i<20; $i++)
		{
			$sql = "INSERT INTO `$dbName`.`test` (`id`, `text`) VALUES ('$i', 'item $i')";
			mysql_query($sql);
		}
	}
	
	function nukeDatabase($dbName)
	{
		$sql = "DROP DATABASE `$dbName`;";
		mysql_query($sql);
	}

	$host = "localhost";
	$userName="root";
	$password="********";
	$dbName = "cp_qa_test";
 
    $con = mysql_connect($host,$userName,$password) or die("Unable to connect");
	createDatabase($dbName);
	
    $page = 1;
    if(isset($_GET['page']))
        $page = $_GET['page'];
 
	$numPerPage = 4;
    $firstRecordNum = ($page-1) * $numPerPage;
    $sql = "SELECT * FROM test LIMIT $firstRecordNum,$numPerPage";
    $resultSet = mysql_query($sql);

	printf("Showing %d records, starting at record #%d<br>", $numPerPage, $firstRecordNum);
	while ( $curRow = mysql_fetch_row($resultSet) )
	{
		printf("<span class='heading'>ID:</span> %d -- <span class='heading'>Text:</span> %s<br>", $curRow[0], $curRow[1]);
	}
	nukeDatabase($dbName);
?>
</body>
</html>
 
Share this answer
 
v2
Comments
Rawan S 14-Oct-13 15:41pm    
the same thing happened!:(
when I use "SELECT * FROM test LIMIT $firstRecordNum,$numPerPage";
but when I add a where condition like:"Select * From test Where (Instructor-Email = '. $_SESSION['ID'] .') Or (Instructor-Email Is Null) LIMIT $firstRecordNum,$numPerPage "
the query don't retrieve any row!
(note: I'm sure the query: Select * From test Where (Instructor-Email = '. $_SESSION['ID'] .') Or (Instructor-Email Is Null) is working. I'm really sorry for bothering you I spend almost two days on trying but with no result --I'm really confused what will the wrong be!-- )
enhzflep 14-Oct-13 19:31pm    
I would try printing out the sql string before you execute it - I think you may get a surprise.

I think if we assume $_SESSION['ID'] is "enhzflep@mail.com", that the code:

(Instructor-Email = '. $_SESSION['ID'] .')

Will actually end up equating to "(Instructor-Email = '.enhzflep@mail.com.')", when I think what you actually want is for it to be without the .s
i.e
(Instructor-Email = '$_SESSION['ID']')
or
(Instructor-Email = '" . $_SESSION['ID'] . "')

which would give you

"(Instructor-Email = 'enhzflep@mail.com')"

Since variables contained within "" quotes are evaluated, but those in '' quotes aren't. Since the string is originally started with "" ones, you can simply type $var and it will swap those 4 characters for the number or string that $var holds. I think you'll find that to be the problem, is where you've used the dot operators (.) to add the string and the variable together, you haven't needed it and it (the .) has been inserted into the computed string itself, rather than joining the string and the text that $_SESSION['ID'] holds.

Like I say - printing out the final sql string before you run the query can be worth trying.
You can always then copy this computed string and past it into phpMyAdmin and see what the error message is.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900