1

我有一个简单的程序,我正在尝试实现某种分页/功能来浏览 MySQL 数据库中的各个记录。代码本身调用一个函数,该函数返回一个关联数组,以便在删除创建非顺序索引的情况下可以顺序导航记录。

function getKeys($handle, $user, $password) {

    try {

         $conn = new PDO($handle,$user,$password);
         $conn -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        }

    catch(PDOException $e) {
        echo "Error connectiong to database. Error: (" . $e -> getMessage() . ")";

    }

    $sql = "Select Workstation_ID from Workstation"; 

    $result = $conn -> query($sql);
    $resultArray = array();

    while ( $row = $result -> fetch()) {

        $resultArray[] = $row;
    }

    $conn = null;

    return $resultArray; } 

我试图将这个函数的结果存储到一个变量中,并从那里尝试增加该变量以用于其他函数:

    $Keys = getKeys($dsn,$un,$pw);

    $i = 0;

    $currID = $Keys[$i][0];

    $row = getResultSet($dsn,$un,$pw,$currID); 

然后我会使用 $row 来显示当前的工作站:

    echo "<hr class='viewHR'>";
    echo "</br></br><div class='viewFormat'>";
    echo "<form name = 'updateWorkstationForm' action ='updateWorkstation.php' method  ='post'>";
    echo "<b>Workstation Name:</b><br><input type = 'Textbox' name = 'pcName' value = '" . $row['Workstation_Name'] . "'/></br>";
    echo "<b>Serial Number: </b><br> <input type = 'Textbox' name = 'SN' value = '" . $row['Serial_Number'] . "'/></br>";

    echo "<b>Model</b></br>";

    echo "<select name ='modelSelect'>";
    echo "<option value = '".$row['Model_ID'] . "'>" .  $row['Model'] . "</option>";
    echo "</select></br>";

    echo "<b>Department</b></br>";
    echo "<select name ='DepartmentSelect'>";
    echo "<option value = '".$row['Department_ID'] . "'>" .  $row['Department'] . "   </option>";
    echo "</select></br>";

我想知道我是否完全错误地解决了这个问题,或者我将如何增加数组的索引以在单击锚标记或按钮时显示每条记录整个文件如下:

<html>
<body>
    <div>
        <?php

            $un = "xxx";
            $pw = "xxxxxx";
            $dsn = "mysql:host=127.0.0.1;dbname=xxxxxxxxxxx";

            $Keys = getKeys($dsn,$un,$pw);

            $i = 0;

            $currID = $Keys[$i][0];

            $row = getResultSet($dsn,$un,$pw,$currID); 

            echo "<hr class='viewHR'>";
            echo "</br></br><div class='viewFormat'>";
            echo "<form name = 'updateWorkstationForm' action ='updateWorkstation.php' method = 'post'>";
            echo "<b>Workstation Name:</b><br> <input type = 'Textbox' name = 'pcName' value = '" . $row['Workstation_Name'] . "'/></br>";
            echo "<b>Serial Number: </b><br> <input type = 'Textbox' name = 'SN' value = '" . $row['Serial_Number'] . "'/></br>";

            echo "<b>Model</b></br>";
            echo "<select name ='modelSelect'>";
            echo "<option value = '".$row['Model_ID'] . "'>" .  $row['Model'] . "</option>";
            echo "</select></br>";

            echo "<b>Department</b></br>";
            echo "<select name ='DepartmentSelect'>";
            echo "<option value = '".$row['Department_ID'] . "'>" .  $row['Department'] . "</option>";
            echo "</select></br>";

            echo "<b>Room</b></br>";
            echo "<select name ='RoomSelect'>";
            echo "<option value = '".$row['Room_ID'] . "'>" .  $row['Room'] . "</option>";
            echo "</select></br>";

            echo "<b>Property Status</b> </br>";
            echo "<select name = 'propertyStatus'>";
            echo "<option value = '".$row['Property_Status_ID'] . "'>" .  $row['Property_Status'] . "</option>";
            echo "</select></br>";

            if ($row['Property_Status'] != "Owned"){
                echo "<b>Lease Company:</b> ";
                echo "<select name = leaseSelect>";
                echo "<option value = '" . $row['Lease_Info_ID'] ."'>Company:" . $row['Company'] . ", Start: " . $row['Start_Date'] . "End: " .$row['End_Date'] . "</option>";
                echo "</select></br>";
            }

            echo "<b>Cart</b></br>";
            echo "<select name ='cartSelect'>";
            echo "<option value = '".$row['Cart_ID'] . "'>" .  $row['Cart_Type'] . "</option>";
            echo "</select></br>";

            echo "<b>Workstation Comments: </b><br> <Textarea rows='5' cols='60' name = 'wsComments'> ". $row['Workstation_Comment'] . " </Textarea></br>";

            echo "<b>Location Comments: </b><br> <Textarea rows='5' cols='60' name = 'locComments'> ". $row['Workstation_Comment'] . " </Textarea></br>";
            echo "<input type = 'submit' value = 'Update' />";
            echo "<input type = 'button' value = 'Cancel' onclick = 'location.reload(this);' />";
            echo "</form>";
            echo "</div>"; 

            /*Function to return a parallel array. This is so that non-sequential records in the database may be described sequentially with the help of an array's indices*/
            function getKeys($handle, $user, $password) {

                try {

                    $conn = new PDO($handle,$user,$password);
                    $conn -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                }

                catch(PDOException $e) {

                    echo "Error connectiong to database. Error: (" . $e -> getMessage() . ")";
                }

                $sql = "Select Workstation_ID from Workstation"; 

                $result = $conn -> query($sql);
                $resultArray = array();

                while ( $row = $result -> fetch()) {

                    $resultArray[] = $row;
                }

                $conn = null;

                return $resultArray; 
            }


            function getResultSet($handle, $user, $password, $ID) {

                $resultSet = "";

                try {

                    $conn = new PDO($handle,$user,$password);
                    $conn -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                }

                catch(PDOException $e) {

                    echo "Error connectiong to database. Error: (" . $e -> getMessage() . ")";
                }

                $sql = "Select Workstation.Workstation_ID,Workstation.Model_ID,Workstation.Property_Status_ID,workstation.Lease_Info_ID, Workstation.Workstation_Name, Workstation.Serial_Number, Model.Model, Department.Department,Room.Room,Property_Status.Property_Status,Lease_Info.Start_Date,Lease_Info.End_Date,Lease_Info.Company,Lease_Info.Lease_Comment,Cart.Cart_Type,Workstation.Workstation_Comment,Workstation.Location_Comment from Workstation INNER JOIN Model ON Workstation.Model_ID = Model.Model_ID  INNER JOIN Department ON Workstation.Department_ID = Department.Department_ID INNER JOIN Room ON Workstation.Room_ID = Room.Room_ID INNER JOIN Property_Status ON Workstation.Property_Status_ID = Property_Status.Property_Status_ID INNER JOIN Lease_Info ON Workstation.Lease_Info_ID = Lease_Info.Lease_Info_ID INNER JOIN Cart ON Workstation.Cart_ID = Cart.Cart_ID where Workstation_ID = :ID"; 

                $pstmt = $conn -> prepare($sql);

                if(!$pstmt) {
                    echo "Error preparing the statement. Error: (" . $conn -> ErrorInfo() . ")";
                }

                $pstmt -> bindParam(':ID', $ID);

                try {

                    $pstmt -> execute();
                }

                catch(PDOException $e) {

                    echo "Failed to execute prepared Statement. Error:  (" . $e -> getmessage() . ")";
                }

                $resultSet = $pstmt -> fetch();

                return $resultSet;

                $conn = null;
            }
        ?>  

    </div>
</body>
</html>

任何批评、见解或指示将不胜感激。

4

1 回答 1

2

如果您只想显示一个子集或仅显示一个子集,则不应获取所有记录。

要分页,请使用LIMIT从句。因此,如果您将记录分成十页,那么要获取第一页,您的查询将是:

SELECT * FROM workstations LIMIT 0,10

其中第一个数字是偏移量,第二个数字是您希望获取的偏移量之后的记录数。要获取第二页,您需要将 limit 子句更改为LIMIT 10,10; 获取第三页LIMIT 20,10,依此类推。PHP方程是:

$offset = (($page - 1) * $records_per_page);

页面值可以来自$_GET变量,例如http://www.example.com/?page=1

其次,如果您只想显示一条记录,请获取该记录:

SELECT * FROM workstations WHERE id = ? LIMIT 1

再次通过$_GET参数传递 ID,并使用 PDO 进行绑定,以避免 SQL 注入漏洞:

<?php

$sql = "SELECT * FROM workstations WHERE id = :id LIMIT 1";

$sth = $db->prepare($sql);
$sth->bindParam(':id', $_GET['id'], PDO::PARAM_INT);
$sth->execute();
$row = $sth->fetchObject();
于 2013-03-04T21:30:42.630 回答