簡體   English   中英

從php mysql_ *到mysqli

[英]Going from php mysql_* to mysqli

很多人一直在告訴我克服mysql_ *並找到mysqli或PDO。

首先,我選擇了Mysqli,因為它看起來非常類似。

但是我在轉換我的網站時遇到了問題。

我似乎無法找到相應的獲取我的數據: mysql_result($result, $i, 'COL 2')

下面的代碼就是現在的樣子,但是我似乎找不到像使用mysql_ *那樣獲取數據的方法。

我這樣做:

<?php 
    $sql="SELECT * FROM items"; 
    $result=mysqli_query($GLOBALS["___mysqli_ston"], $sql); 

     for ($i = $start; $i < $end; $i++) { 
        if ($i == $total_results) { 
            echo ' 
            <div class="col-sm-12 col-lg-12 col-md-12"><h4>Der er ingen produkter at vise</h4></div> 
            '; 
            break; 
        } 
        echo ' 
        <div class="col-sm-4 col-lg-4 col-md-4" style="min-height:425px;"> 
                        <div class="thumbnail"> 
                            <img src="'.mysql_result($result, $i, 'COL 25').'" alt="" style="max-height:300px;"> 
                            <div class="caption"> 
                                <h4 class="pull-right">'.mysql_result($result, $i, 'COL 20').' point</h4> 
                                <h4 style="color:rgb(220,145,27)">'.mysql_result($result, $i, 'COL 2').'</h4> 
                                <p>Vare nr.: '.mysql_result($result, $i, 'COL 14').'</p> 
                            </div> 
                            <div class="buy"> 
                                <form action="" method="POST" name="buy"> 
                                    <!--- <select name="variant" class="form-control"> 
                                        <option>small</option> 
                                    </select><br> --> 
                                    <button class="btn btn-m center-block" type="submit" style="color:white;background-color:rgb(220,145,27);">Køb</button> 
                                </form> 
                            </div> 
                        </div> 
                    </div> 
        '; 
    }; 
    ?> 

簡單地說, mysql_result沒有等價物,這意味着你必須重新構建你的代碼。 您可以使用whileforeach -loop遍歷所有行。

<?php
$mysqli = $GLOBALS["___mysqli_ston"];
$sql = "SELECT * FROM items";

if ($result = mysqli_query($mysqli, $sql)) {
    // Query passed, let's continue
    if (mysqli_num_rows($result) > 0) {
        // We have results! Continue
        while ($row = mysqli_fetch_array($query)) {
            echo '<img src="'.$row[24].'" alt="" style="max-height:300px;"><div class="caption"> 
                <h4 class="pull-right">'.$row[20].' point</h4> 
                <h4 style="color:rgb(220,145,27)">'.$row[2].'</h4> 
                <p>Vare nr.: '.$row[4].'</p> 
                </div> ';
        }
    } else {
        echo "No results";
    }
} else {
    echo "Query failed";
}
?> 

您也可以使用$row['columname'] ,這在閱讀您實際嘗試輸出的代碼時更容易。

如果您已經開始使用mysqli_result ,那么您可以創建自己的函數,該函數大致相同( 來自此PHP.net注釋 )。 (就個人而言,我會選擇上面的例子)。

function mysqli_result($res, $row, $field=0) { 
    $res->data_seek($row); 
    $datarow = $res->fetch_array(); 
    return $datarow[$field]; 
}

在任何情況下,我強烈建議您不要在全局變量中傳遞數據庫連接,而是包含數據庫參數並為每個腳本創建連接。

暫無
暫無

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

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