从参考表中检索记录

时间:2013-02-14 03:56:48

标签: php mysql phpmyadmin

我有这个分页代码,实际上它将检索我的表学生中的所有记录,我想要的是检索带有年级参考的记录。这是代码:

感谢您的帮助!

<?php

if (!isset($_POST['level'])) {
    $_POST['level'] = "undefine";
}
$level = $_POST['level'];

mysql_connect("localhost", "root", "") or die(mysql_error());

mysql_select_db("kp_and_harang") or die(mysql_error());

if (!(isset($_GET['pagenum']))) {
    $pagenum = 1;
} else {
    $pagenum = $_GET['pagenum'];
}
$data = mysql_query("SELECT id,surname,firstname,middlename,level FROM students") or die(mysql_error());

$rows = mysql_num_rows($data);
$page_rows = 2;
$last = ceil($rows / $page_rows);

if ($pagenum < 1) {
    $pagenum = 1;
} elseif ($pagenum > $last) {
    $pagenum = $last;
}

$max = 'limit ' . ($pagenum - 1) * $page_rows . ',' . $page_rows;
$data_p = mysql_query("SELECT *,LPAD(id,4,'0') as id FROM students $max ") or die(mysql_error());

while ($info = mysql_fetch_array($data_p)) {
    echo "<tr class='tr1'>";
    echo "<center>";
    echo "<td class='1'>" . $info['id'] . "</td>";
    echo "<td class='1'>" . $info['surname'] . ", " . $info['firstname'] . " " . $info['middlename'] . "</td>";
    echo "<td class='1'>" . $info['level'] . "</td>";
    echo "</center>";
    echo "</tr>";

    echo "<br>";
}
echo "<p>";
echo " --Page $pagenum of $last-- <p>";
if ($pagenum == 1) {

} else {
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
    echo "----";
    $previous = $pagenum - 1;
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}

echo " ---- ";

if ($pagenum == $last) {

} else {
    $next = $pagenum + 1;
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
    echo "----";
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
}
?> 

1 个答案:

答案 0 :(得分:0)

我已重写您的代码以使用mysqli。

同样,您只需要在SQL查询中添加“where”。

<?php

if (!isset($_POST['level'])) {
    $_POST['level'] = "undefined";
}

$mysqli = new mysqli("localhost", "user", "password", "database");

$level = $mysqli->real_escape_string($_POST['level']);

if (!(isset($_GET['pagenum']))) {
    $pagenum = 1;
} else {
    $pagenum = $_GET['pagenum'];
}
$query = "SELECT id,surname,firstname,middlename,level FROM students";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);

$rows = $result->num_rows;
$page_rows = 2;
$last = ceil($rows / $page_rows);

if ($pagenum < 1) {
    $pagenum = 1;
} elseif ($pagenum > $last) {
    $pagenum = $last;
}

$max = 'limit ' . ($pagenum - 1) * $page_rows . ',' . $page_rows;
if ($level == "undefined"){
$query = "SELECT *,LPAD(id,4,'0') as id FROM students $max" ORDER BY level;}
else{
$query = "SELECT *,LPAD(id,4,'0') as id FROM students $max where level = '$level' ORDER BY level";}
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
while ($info = $result->fetch_array(MYSQLI_BOTH)) {
    echo "<tr class='tr1'>";
    echo "<center>";
    echo "<td class='1'>" . $info['id'] . "</td>";
    echo "<td class='1'>" . $info['surname'] . ", " . $info['firstname'] . " " . $info['middlename'] . "</td>";
    echo "<td class='1'>" . $info['level'] . "</td>";
    echo "</center>";
    echo "</tr>";

    echo "<br>";
}
echo "<p>";
echo " --Page $pagenum of $last-- <p>";
if ($pagenum == 1) {

} else {
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
    echo "----";
    $previous = $pagenum - 1;
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}

echo " ---- ";

if ($pagenum == $last) {

} else {
    $next = $pagenum + 1;
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
    echo "----";
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
}
?> 

同样,这段代码的目的是尽可能地匹配你的代码,但是...更好的方法是将level设为int值,并使用默认情况下为{{1}行的情况}。