将查询转换为预准备语句 - 同时循环特定问题

时间:2014-09-22 22:43:19

标签: php mysql sql

我刚开始学习php并在

创建了一个漂亮的小页面

http://www.dowricks.com/index.php?route=information/yeast

我现在想将我的查询转换为准备好的陈述,并且没有运气就尝试了所有内容。

我要转换的部分如下 -

$q = 'SELECT yeast, rating, description, weblink
, image, sideimage 
FROM dowdb_yeast_selector 
WHERE fruit = "' . $_POST['fruit'] . '" 
ORDER BY rating DESC';  
            $r = @mysqli_query ($dbc, $q); // Run the query.

            // Present the results
            echo '<table width="100 px" cellspacing="15" cellpadding="15">';// Table header:
            echo '<tr bgcolor="green"><td colspan="5"><div class="yeast_heading" align="center"><em><b>The following yeasts are suitable for fermenting ' . $_POST['fruit'] . '</b></em></div></td></tr>';
            echo '<tr bgcolor="green"><td align="left">Yeast</td><td align="left">Rating</td><td align="left">Description</td> <td></td><td></td></tr>';
            // Fetch and print all the records....
            $bg = '#676767'; 
                    while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) 
                    {
                        $sideimage = $row['sideimage'];
                        if ($row['rating'] == 3)
                        {$rating = "Best";}
                        if ($row['rating'] == 2)
                        {$rating = "Better";}
                        if ($row['rating'] == 1)
                        {$rating = "Good";}
                    $bg = ($bg=='#222222' ? '#676767' : '#222222');
                    echo '<tr bgcolor="' . $bg . '"><td align="left">' . $row['yeast'] . '</td><td><div align="center">' . $rating . '</div></td><td align="left">' . $row['description'] . '</td> <td><a href="' . $row['weblink'] . '" title="Go find">Buy</a></td><td><img src="' . $row['image'] . '" width="100" height="100" alt="Vintners Harvest Yeast" longdesc="' . $row['image'] . '" /></tr>';

如果有人能为我翻译,我将非常感激。 6个小时并且厌倦了。

如果您需要任何其他信息,请与我们联系。

保罗

1 个答案:

答案 0 :(得分:0)

<?php
$db = new PDO('mysql:host=HOST;dbname=DATABASE', $user, $pass);

$stmt = $db->prepare('
    SELECT
        yeast,
        rating,
        description,
        weblink,
        image,
        sideimage 
    FROM dowdb_yeast_selector 
    WHERE
        fruit = :fruit
    ORDER BY
        rating DESC
');

$stmt->bindParam(':fruit',$_POST['fruit'],PDO::PARAM_STR,50);

$stmt->execute();

while ($row = $stmt->fetch())
    print_r($row);

我想你知道如何操纵$row