如何正确地将mySQL查询转换为PDO

时间:2015-09-11 11:16:58

标签: php mysql 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
}

提前致谢

2 个答案:

答案 0 :(得分:1)

我发现我的问题是类别中没有显示的条目。

在我的查询中,我尝试了SELECT * WHERE field1 = .$variable.

但我需要将变量绑定为参数,如下所示:

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

这已按预期成功填充了我的类别。

值得注意的是,你的常识的答案是正确的,但只回答了我的部分问题。阅读这些答案的任何人都应该注意到这两个答案。

EDIT ......

正如ShowDev所指出的那样  &#39; if($ stmt-&gt; fetchColumn()&gt; 0)&#39;  condition将计数提前到第二行,然后仅返回剩余的3条记录。

ShowDev发布的链接显示了此类查询的正确程序

答案 1 :(得分:-1)

摆脱if ($stmt->fetchColumn() > 0)条件

这个无用的消息改为此

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