简体   繁体   English

MySQLi不按顺序返回行

[英]MySQLi not returning rows in order

This is probably not a very difficult question to answer. 这可能不是一个很难回答的问题。 I'm having trouble with this PHP function I wrote... it returns the rows line by line, but it's returning them incremented by 4 each time. 我在编写此PHP函数时遇到了麻烦...它逐行返回行,但每次返回的行数都递增4。 So the the 1st row will output, then the 5th, then the 9th... 因此,将输出第一行,然后输出第五行,然后输出第九行...

function showDatabases() {

# $_GET variables from the URL.
$database = mysql_real_escape_string($_GET['database']);
$table    = mysql_real_escape_string($_GET['table']);   

$mysqli = new mysqli('127.0.0.1', 'root', '', $database);

$query_one = $mysqli->query("SELECT * from $table");

$num_rows   = mysqli_num_rows($query_one);
$num_fields = mysqli_num_fields($query_one);

    for ($x = 0; $x < $num_rows; $x++) {
       for ($c = 0; $c < $num_fields; $c++) {
            $row = mysqli_fetch_row($query_one);
            echo($row[$c]."&nbsp;&nbsp;");
       }
    echo("<br/>");
    }
}

Thanks! 谢谢!

mysqli_fetch_row fetched an entire row and moves the pointer to the following row. mysqli_fetch_row提取了整行并将指针移至下一行。 You should call it only once per each row; 每行只能调用一次; now you are calling it once per column. 现在您每列调用一次。

That is, 那是,

for ($x = 0; $x < $num_rows; $x++) {
   $row = mysqli_fetch_row($query_one);
   for ($c = 0; $c < $num_fields; $c++) {
        echo($row[$c]."&nbsp;&nbsp;");
   }
echo("<br/>");
}

you are complicating things you can do it with just one loop 您使事情变得复杂,只需一个循环即可完成

$query = "SELECT * from $table";
if ($result = $mysqli->query($query)) {
    while ($row = $result->fetch_assoc()) {
        echo $row['fieldname']."&nbsp;&nbsp;";
    }
}

I advice you to add order by some time the default order is not the id order. 我建议您在一段时间内添加订单,默认订单不是id订单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM