簡體   English   中英

如何將MySQL查詢正確轉換為PDO

[英]How do I properly convert a mySQL query to PDO

我正在嘗試將mySQL查詢轉換為PDO。

我部分獲得了預期的數據,但是我無法獲得整個查詢來提取正確的數據。 mySQL查詢能夠提取4個類別,並在每個類別中提取並顯示適當數量的條目。 PDO轉換查詢僅提取3個類別,而沒有任何類別的條目

我假設我沒有正確轉換查詢,但是找不到問題所在。

我還想提供一些有關如何使用新代碼限制SQL注入的公開性的投入。

老查詢(工作中)

function listPuppies(){

    include("db_connect.php");

    $query = "  SELECT *
                FROM tblLitters
                WHERE available = 1
                ORDER BY litBreed, litMother";
    $resultOut = mysql_query($query, $connection) or die ("<br>Error in query: $query.".mysql_error($connection));

    //Check if a row is returned
    if (mysql_num_rows($resultOut) > 0) {

        while($rowOut = mysql_fetch_array($resultOut)){

            $litterID           = $rowOut['litterID'];
            $litMother          = $rowOut['litMother'];
            $litBreed           = $rowOut['litBreed'];
            $litBreedDate       = $rowOut['litBreedDate'];
            $litDesc            = $rowOut['litDesc'];
            $litterImage        = $rowOut['litImage'];
            $litterImageThumb   = $rowOut['litterImageThumb'];
            $litBreedCost       = $rowOut['litBreedCost'];

            if ($litterImageThumb == ''){
                $litterPic = "";
            }else{
                $litterPic = "<img src=\"images/Litters/".$litterImageThumb."\" align=\"right\" style=\"padding:1px; margin:3px; border:6px solid #fff;\">";
            }

            echo "<table width=\"650\"><tr>\n";
            if ($breed <> $rowOut['litBreed']){
                $breed = $rowOut['litBreed'];
                echo "</tr></table>\n";
                echo "<br><br><div class=\"breedHead\">$breed's For Sale</div><hr color=\"#C5FBB4\">\n";
                echo "<br><table width=\"650\" cellspacing=\"0\" cellpadding=\"5\"><tr><td colspan=\"3\"><table bgcolor=\"#044726\" width=\"100%\" border=\"1\" bordercolor=\"#137b48\" cellpadding=\"6\"><tr><td>".$litterPic."<span style=\"font-size:12pt;\">".$litDesc."<br><br><strong>Mother:</strong> $litMother<br><strong>Cost: </strong>$$litBreedCost<br><br></span></td></tr></table></td></tr><tr><td colspan=\"3\">&nbsp;</td></tr>";
                $counter = 0;
            }else{
                if ($pupLitterID <> $rowOut['litterID']){
                    echo "</table>\n";
                    echo "<br><br><br><table width=\"650\" cellspacing=\"0\" cellpadding=\"5\"><tr><td colspan=\"3\"><table bgcolor=\"#044726\" width=\"100%\" border=\"1\" bordercolor=\"#137b48\" cellpadding=\"6\"><tr><td>".$litterPic."<span style=\"font-size:12pt;\">".$litDesc."<br><br><strong>Mother:</strong> $litMother<br><strong>Cost: </strong>$$litBreedCost<br><br></span></td></tr></table></td></tr><tr><td colspan=\"3\">&nbsp;</td></tr>";
                    $counter = 0;
                }
            }





            $query = "  SELECT *
                        FROM tblPuppies
                        WHERE litterID = $litterID";
            $result = mysql_query($query, $connection) or die ("<br>Error in query: $query.".mysql_error($connection));

            //$breed = $row['pupBreed'];
            $counter = 0;



            //Check is a row is returned
            if (mysql_num_rows($result) > 0) {

                //old table start
                while($row = mysql_fetch_array($result)){
                    $status = $row['pupStatus'];
                    $pupLitterID = $row['litterID'];

                    if ($status == "For Sale"){
                        if ($row['pupOnHold'] == 1){
                            $status = '<font color=\"red\">On Hold</font>';
                        }
                        if ($row['pupSold'] == 1){
                            $status = '<font color=\"red\">Sold</font>';
                        }
                    }
                    if ($row['pupSex'] == 'F'){
                        $sex = 'Female';
                    }else{
                        $sex = 'Male';
                    }

                    //used to change popup window position depending on where thumbnail is palced on page
                    if ($counter == 0){
                        echo "<td width=\"33%\"><a class=\"thumbnailLeft\" href=\"#thumb\">";
                    }
                    if ($counter == 1){
                        echo "<td width=\"33%\"><a class=\"thumbnail\" href=\"#thumb\">";
                    }
                    if ($counter == 2){
                        echo "<td width=\"33%\"><a class=\"thumbnailRight\" href=\"#thumb\">";
                    }
                    echo "<div align=\"center\"><img src=\"images/ForSale/".$row['pupPicThumb']."\" style=\"padding:1px; border:6px solid #fff;\"><br>".$row['pupName']." - $sex<br><strong>$status</strong></div><span><img src=\"images/ForSale/".$row['pupPic']."\"></span></a><div align=\"center\"><a href=\"mailto:sales@adorablepuppies.com.au?Subject=Interest in puppy ".$row['pupName']."\">Contact Us About This Pup</a></div></td>";

                    if ($counter == 2){
                        echo "</tr><tr>\n";
                        $counter = -1;

                        if ($breed <> $rowOut['litBreed']){
                            $breed = $rowOut['litBreed'];
                            echo "</table>\n";
                            echo "<br><br><div class=\"breedHead\">$breed's For Sale</div><hr color=\"#C5FBB4\"><br>\n";
                            echo "<table width=\"95%\"><tr><td>".$litterPic."<span style=\"font-size:12pt;\">".$litDesc."<br><br><strong>Mother:</strong> $litMother<br><strong>Litter Birth Date: </strong>$litBreedDate<br><br></span></td></tr></table>";
                            echo "<table width=\"650\"><tr>\n";
                            $counter = -1;
                        }
                    }               
                    $counter = $counter + 1;                        
                }
                echo "</tr></table>\n";
            }else{
                echo "There are no puppies left for sale in this litter, sorry.<br><br>Please check back again soon.";
            }// End IF/ELSE 





        }//end outer while

    }else{
        echo "There are currently no puppies for sale.<br>Please check back again soon.";
    }//end outer if
}

新查詢(無法完全運行)

function listPuppies(){

include("db_connect.php");

    try {
$stmt = $connection->prepare("SELECT * FROM tblLitters WHERE available = 1");
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
$stmt->execute();
}

//Catch PDO Query Error
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}

// set the resulting array to associative
//$result = $stmt->fetch(PDO::FETCH_ASSOC);
if ($stmt->fetchColumn() > 0) {

    foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $rowOut){

            $litterID           = $rowOut['litterID'];
            $litMother          = $rowOut['litMother'];
            $litBreed           = $rowOut['litBreed'];
            $litBreedDate       = $rowOut['litBreedDate'];
            $litDesc            = $rowOut['litDesc'];
            $litterImage        = $rowOut['litImage'];
            $litterImageThumb   = $rowOut['litterImageThumb'];
            $litBreedCost       = $rowOut['litBreedCost'];

            if ($litterImageThumb == ''){
                    $litterPic = "";
                }else{
                    $litterPic = "<img src=\"images/Litters/".$litterImageThumb."\" align=\"right\" style=\"padding:1px; margin:3px; border:6px solid #fff;\">";
                }

            echo "<table width=\"600\"><tr>\n";
            if ($breed <> $rowOut['litBreed']){
                $breed = $rowOut['litBreed'];
                echo "</tr></table>\n";
                echo "<br><br><div class=\"breedHead\">$breed's For Sale</div><hr color=\"#C5FBB4\">\n";
                echo "<br><table width=\"600\" cellspacing=\"0\" cellpadding=\"5\"><tr><td colspan=\"3\"><table bgcolor=\"#044726\" width=\"100%\" border=\"1\" bordercolor=\"#137b48\" cellpadding=\"6\"><tr><td>".$litterPic."<span style=\"font-size:12pt;\">".$litDesc."<br><br><strong>Mother:</strong> $litMother<br><strong>Cost: </strong>$$litBreedCost<br><br></span></td></tr></table></td></tr><tr><td colspan=\"3\">&nbsp;</td></tr>";
                $counter = 0;
            }else{
                if ($pupLitterID <> $rowOut['litterID']){
                    echo "</table>\n";
                    echo "<br><br><br><table width=\"600\" cellspacing=\"0\" cellpadding=\"5\"><tr><td colspan=\"3\"><table bgcolor=\"#044726\" width=\"100%\" border=\"1\" bordercolor=\"#137b48\" cellpadding=\"6\"><tr><td>".$litterPic."<span style=\"font-size:12pt;\">".$litDesc."<br><br><strong>Mother:</strong> $litMother<br><strong>Cost: </strong>$$litBreedCost<br><br></span></td></tr></table></td></tr><tr><td colspan=\"3\">&nbsp;</td></tr>";
                    $counter = 0;
                }
            }

            $stmt1 = $connection->prepare("SELECT * FROM tblPuppies WHERE litterID = .$litterID.");

            $counter = 0;

            //Check if a row is returned
            if ($stmt1->fetchColumn() > 0) {

                //old table start
                foreach ($stmt1->fetch(PDO::FETCH_ASSOC) as $rowOut){
                    $status = $row['pupStatus'];
                    $pupLitterID = $row['litterID'];

                    if ($status == "For Sale"){
                        if ($row['pupOnHold'] == 1){
                            $status = '<font color=\"red\">On Hold</font>';
                        }
                        if ($row['pupSold'] == 1){
                            $status = '<font color=\"red\">Sold</font>';
                        }
                    }
                    if ($row['pupSex'] == 'F'){
                        $sex = 'Female';
                    }else{
                        $sex = 'Male';
                    }

                    //used to change popup window position depending on where thumbnail is placed on page
                    if ($counter == 0){
                        echo "<td width=\"33%\"><a class=\"thumbnailLeft\" href=\"#thumb\">";
                    }
                    if ($counter == 1){
                        echo "<td width=\"33%\"><a class=\"thumbnail\" href=\"#thumb\">";
                    }
                    if ($counter == 2){
                        echo "<td width=\"33%\"><a class=\"thumbnailRight\" href=\"#thumb\">";
                    }
                    echo "<div align=\"center\"><img src=\"images/ForSale/".$row['pupPicThumb']."\" style=\"padding:1px; border:6px solid #fff;\"><br>".$row['pupName']." - $sex<br><strong>$status</strong></div><span><img src=\"images/ForSale/".$row['pupPic']."\"></span></a><div align=\"center\"><a href=\"mailto:sales@adorablepuppies.com.au?Subject=Interest in puppy ".$row['pupName']."\">Contact Us About This Pup</a></div></td>";

                    if ($counter == 2){
                        echo "</tr><tr>\n";
                        $counter = -1;

                        if ($breed <> $rowOut['litBreed']){
                            $breed = $rowOut['litBreed'];
                            echo "</table>\n";
                            echo "<br><br><div class=\"breedHead\">$breed's For Sale</div><hr color=\"#C5FBB4\"><br>\n";
                            echo "<table width=\"95%\"><tr><td>".$litterPic."<span style=\"font-size:12pt;\">".$litDesc."<br><br><strong>Mother:</strong> $litMother<br><strong>Litter Birth Date: </strong>$litBreedDate<br><br></span></td></tr></table>";
                            echo "<table width=\"600\"><tr>\n";
                            $counter = -1;
                        }
                    }               
                    $counter = $counter + 1;                        
                }
                echo "</tr></table>\n";
            }else{
                echo "There are no puppies left for sale in this litter, sorry.<br><br>Please check back again soon.";
            }// End IF/ELSE 





        }//end outer while

    }else{
        echo "There are currently no puppies for sale.<br>Please check back again soon.";
    }//end outer if
}

提前致謝

我發現類別中的條目沒有顯示我的問題。

在我的查詢中,我嘗試了SELECT * WHERE field1 = .$variable.

但是我需要將變量綁定為參數,如下所示:

$stmt1 = $connection->prepare("SELECT * FROM tblPuppies WHERE litterID = :litterID"); 
            $stmt1->bindParam(':litterID', $litterID);
            $stmt1->execute();

這成功地按預期填充了我的類別。

值得注意的是,您的常識答案是正確的,但是僅回答了我的部分問題。 閱讀這些答案的其他任何人都應注意兩個答案。

編輯......

如ShowDev所指出的,“ if($ stmt-> fetchColumn()> 0)”條件使計數前進到第二行,然后僅返回剩余的3條記錄。

ShowDev發布的鏈接顯示了此類查詢的正確過程

擺脫if ($stmt->fetchColumn() > 0)條件

對於這個無用的消息更改為此

$found = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($found) {
    foreach ($found as $rowOut){

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM