You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP&MySQLi技术问题:如何获取同列其他行的数据

How to Fetch and Display Multiple Rows from a MySQLi Column

Hey there! Let's break this down simply since you're already getting the hang of single-row queries—great start by the way! 😊 It sounds like you're stuck on moving beyond just one row, so let's cover a few easy ways to grab those later rows in your picture column and show their links.

Basic Approach: Fetch All Rows in the Column

If you want to display all the image links in the picture column, you can use a loop to iterate through every row returned by your query. Here's a simple code example:

// Assuming you already have your MySQLi connection set up ($conn)
$query = "SELECT picture FROM your_table_name"; // Replace with your actual table name
$result = mysqli_query($conn, $query);

// Check if there are results
if (mysqli_num_rows($result) > 0) {
    // Loop through each row
    while ($row = mysqli_fetch_assoc($result)) {
        // Display the image link (you can wrap it in an <img> tag too!)
        echo "Image Link: " . $row['picture'] . "<br>";
        // Or show the image directly:
        // echo '<img src="' . $row['picture'] . '" alt="Uploaded Image"><br>';
    }
} else {
    echo "No images found in the database.";
}

// Don't forget to free the result and close the connection when done
mysqli_free_result($result);
mysqli_close($conn);

Grab Specific "Later" Rows (e.g., Rows 3 and Beyond)

If you only want rows starting from a certain position (like skipping the first 2 rows to get rows 3,4,5...), use the LIMIT clause with an offset. The syntax is LIMIT offset, number_of_rows—the offset is how many rows you want to skip.

Example to get all rows after the first 2:

$query = "SELECT picture FROM your_table_name LIMIT 2, 100"; // Skip first 2, get up to 100 rows
$result = mysqli_query($conn, $query);

// Then loop through the results like before
while ($row = mysqli_fetch_assoc($result)) {
    echo '<img src="' . $row['picture'] . '" alt="Image"><br>';
}

Note: If you don't know how many rows there are, you can use a large number (like 100) as the second value in LIMIT, and it will just return all remaining rows.

Using a WHERE Clause to Target Specific Rows

If you have a unique identifier (like an id column) in your table, you can also use WHERE to get rows where the id is greater than a certain number. For example, to get all rows with id > 5:

$query = "SELECT picture FROM your_table_name WHERE id > 5";
$result = mysqli_query($conn, $query);

// Loop and display as usual
while ($row = mysqli_fetch_assoc($result)) {
    echo $row['picture'] . "<br>";
}

A quick reminder: Always make sure your connection is secure, and if you ever start using user input in queries, use prepared statements to avoid SQL injection—but since you're just fetching rows right now, the examples above should work perfectly for your current needs.

内容的提问来源于stack exchange,提问作者Justin Everett

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:26:10