簡體   English   中英

從php / MySql中的特定列檢索數據

[英]retrieving data from a specific column in php/MySql

我正在使用WordPress和XAMPP。 我正在嘗試使用mysqli_fetch_row()函數從名為ltport_posts的表中從名為post_title的列中獲取帖子標題。 與數據庫的連接工作得很好。 但是,帖子標題似乎沒有寫在新聞自動收報機中。 現在我知道$row變量是一個枚舉數組,所以我們應該寫入偏移數來訪問該列。 應該注意的是while循環正在工作,因為我在ltport_posts中有四行,並且生成了四個<div> (瀏覽器的inspect元素向我顯示)。 但是整個標簽都是空的:

<div class="ticker-wrap">
<div class="ticker">
<div class="ticker__item"> </div>
<div class="ticker__item"> </div>
<div class="ticker__item"> </div>
<div class="ticker__item"> </div>
</div>
</div>

這是php / HTML代碼:

 <div class="ticker">
        <?php $query="SELECT post_title from ltport_posts";
        if($result=mysqli_query($conn,$query))
        {
            while($row=mysqli_fetch_row($result))
            {?>
            <div class="ticker__item"><?php printf("%s", $row[5]); ?> </div>
        <?php }
        mysqli_free_result($result);
    }
    mysqli_close($conn);?>
</div>

mysqli_fetch_row

mysqli_result :: fetch_row - mysqli_fetch_row - 獲取結果行作為枚舉數組

你的查詢是

$query="SELECT post_title from ltport_posts";

您只需從查詢中獲取one column 所以你只得到$row[0]它的索引從0開始

 while($row=mysqli_fetch_row($result))
            {?>
            <div class="ticker__item"><?php printf("%s", $row[0]); ?> </div>
        <?php }

用於獲取多列

$query = "SELECT column1, column2,column3,column4 FROM City ORDER by ID DESC";

if ($result = mysqli_query($link, $query)) {

    while ($row = mysqli_fetch_row($result)) {
        printf ("%s (%s)\n", $row[0], $row[1],$row[3], $row[4]);
    }
}

您可以執行以下操作:

// Prepare the query
$stmt = "select column FROM table";

// Execute it...
$result = mysqli_query($stmt);

// Fetch Results
$row = mysqli_fetch_assoc($result);

// Output single column
echo $row['column'];

當然,您也可以循環使用多個結果。 mysqli_fetch_assoc將返回一個數組,其中鍵是列名。

暫無
暫無

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

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