我在数据库中的while循环是重复结果。 我知道这是我格式不好的查询。

I have a query to bring results from my database. It works... until there are more than 2 results that it, then it just repeats some results before adding in new ones.

I know it will be because my query is fairly poor, can anyone advise me?

The idea is

  1. connect to database with photo links
  2. get the default user picture as $profile_main
  3. join the words "photo_" with the default picture number and call it $answer (ex: column 'photo_1' in database)
  4. now check the database again and get the results for $answer and output all information from that database column.
$result = mysqli_query($con,"SELECT * FROM tbl_photos");
while($row = mysqli_fetch_array($result))
    {
    $profile_main = $row['default'];
    $answer = "photo_" . $profile_main;

        $result2 = mysqli_query($con,"SELECT $answer FROM tbl_photos");
        while($row = mysqli_fetch_array($result2))

        {
            echo "<img src=\"" . $row[0] . "\">";
        }
    }

mysql_fetch_row returns numerical indexes instead of column names (so ['default'] just won't work)...

This is how I would do it if I'm understanding you correctly:

$result = mysqli_query($con,"SELECT * FROM tbl_photos");
while($row = mysqli_fetch_assoc($result))
    {
    $answer = $row['photo_'.$row['default']];
    echo "<img src=\"" . $answer . "\">";
    }

Anyway, this is assuming default and photo_x are in the same row.

If you want only one result for a photo then you can use LIMIT like this

SELECT $answer FROM tbl_photos LIMIT 1

First, both loops you set same $row variable. Use 2 different variable names so that the results don't get mixed up.

Second issue is that you have you have 2 loops , so it will show all results each time. You need to break in the second loop. Like this:

$result = mysqli_query($con,"SELECT * FROM tbl_photos");
while($row = mysqli_fetch_array($result))
    {
    $profile_main = $row['default'];
    $answer = "photo_" . $profile_main;

        $result2 = mysqli_query($con,"SELECT $answer FROM tbl_photos");
        while($row2 = mysqli_fetch_array($result2))

        {
            echo "<img src=\"" . $row2[0] . "\">";
            break;
        }
}

Or by using only one query, it would be much more efficient:

$result = mysqli_query($con,"SELECT * FROM tbl_photos");
while($row = mysqli_fetch_array($result))
    {
    $profile_main = $row['default'];
    $answer = "photo_" . $profile_main;        
    echo "<img src=\"" . $row[$answer] . "\">";

}

You only require 1 query.

TRY

$result = mysqli_query($con,"SELECT * FROM tbl_photos");
while($row = mysqli_fetch_array($result))
    {
    $photo = "photo_" .($row['default'];
    echo "<img src=\"" . $photo . "\">";
    }