如何回显所有列的所有行?

How to echo all rows with all columns?

我可以回显所有行吗?这是我的脚本:

$options=mysql_query("SELECT col1, col2, col3, col4 FROM table1");
while($allrowsandcolumns=mysql_fetch_array($options)) { 
    $options1=mysql_query("SELECT * FROM table2 where id=$allrowsandcolumns");
} 
while($rows=mysql_fetch_array($options1)) { 
    echo $rows['name'];
}

我的错误提示:上面代码第 3 行中的数组到字符串的转换

我猜是因为这不是您显示所有列和行的方式...

我同意 Jay Blanchard 的观点,您应该使用 mysql_* 函数以外的东西。

首先出现错误消息是因为您使用数组代替字符串:

"SELECT * FROM table2 where id=$allrowsandcolumns" 

$allrowsandcolumns 是数据库的结果数组,其中包含第一个查询的 col1、col2、col3、col4。视觉上看起来像这样:

array ( 
    "col1" => value1
    "col2" => value2
    "col3" => value3
    "col4" => value4
)

我认为我们可以同意尝试将数组放入字符串中是行不通的。相反,您可能想要这样的东西:

"SELECT * FROM table2 where id=" . $allrowsandcolumns["col1"]

或与table2匹配的id列为准。

就回显所有行而言...在我看来您是编程新手。但是嵌套很容易解释:

现在你有这个:

$options=mysql_query("SELECT col1, col2, col3, col4 FROM table1");
while($allrowsandcolumns=mysql_fetch_array($options)) { 
    $options1=mysql_query("SELECT * FROM table2 where id=$allrowsandcolumns"); 
} 
while($rows=mysql_fetch_array($options1)) { 
    echo $rows['name'];
}

这不是您认为的那样。它将循环遍历 table1 中的每一行,然后 使用 $options 中的最后一行 当它获取并回显一个名称时。你需要做的是像这样嵌套循环:

$options=mysql_query("SELECT col1, col2, col3, col4 FROM table1");
while($allrowsandcolumns=mysql_fetch_array($options)) { 
    $options1=mysql_query("SELECT * FROM table2 where id= " . $allrowsandcolumns["col1"]);
    while($rows=mysql_fetch_array($options1)) { 
        echo $rows['name'];
    }
} 

话虽如此。这是一个坏主意。循环 SQL 查询对性能来说很糟糕。在您寻求知识的过程中,查找连接查询以便您可以一次检索所有这些结果。像这样:

 SELECT col1,col2,col3,col4 
 FROM table1 JOIN table2 on table2.id = table1.col1

然后您可以对这些值进行一次循环。