Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 逐列打印mysql结果_Php_Mysql - Fatal编程技术网

Php 逐列打印mysql结果

Php 逐列打印mysql结果,php,mysql,Php,Mysql,我觉得自己非常愚蠢,因为我没能弄明白这一点,但我想这就是Stackoverflow的作用。如果有人能向我指出有用的资源或向我解释如何解决这个问题,我将非常感激 基本上,我从一个表中获取几行,现在我需要能够打印出每行字段的值,然后是不同字段中每行的值,依此类推。这很难解释,但让我给你举个例子 假设我们有一个包含三个字段的表:id-name-url。现在,我希望能够按照以下顺序输出结果: 1 2 3 John Steven Patrick http://google.com/ http://stac

我觉得自己非常愚蠢,因为我没能弄明白这一点,但我想这就是Stackoverflow的作用。如果有人能向我指出有用的资源或向我解释如何解决这个问题,我将非常感激

基本上,我从一个表中获取几行,现在我需要能够打印出每行字段的值,然后是不同字段中每行的值,依此类推。这很难解释,但让我给你举个例子

假设我们有一个包含三个字段的表:id-name-url。现在,我希望能够按照以下顺序输出结果:

1
2
3
John
Steven
Patrick
http://google.com/
http://stackoverflow.com/
http://php.net/

如何循环查询结果以实现此目的?

您应该创建一个数组,然后将其输出:

$ids = array();
$names = array();
$urls = array();
while($row = ...){
    $ids[] = $row['id'];
    $names[] = $row['name'];
    $urls[] = $row['url'];    
}

foreach($ids as $id) {
    echo $id . PHP_EOL;
}
//do the same for  the other 2

那么你应该做一个数组,然后把它吐出来:

$ids = array();
$names = array();
$urls = array();
while($row = ...){
    $ids[] = $row['id'];
    $names[] = $row['name'];
    $urls[] = $row['url'];    
}

foreach($ids as $id) {
    echo $id . PHP_EOL;
}
//do the same for  the other 2

您可以通过
union
执行此操作。唯一棘手的部分是确保将所有字段强制转换为公共数据类型(在本例中为文本)。此外,每个子查询都按id排序,以确保排序一致

select id::text
from table
order by id
union
select name::text
from table
order by id
union
select url::text
from table
order by id

您可以通过
union
执行此操作。唯一棘手的部分是确保将所有字段强制转换为公共数据类型(在本例中为文本)。此外,每个子查询都按id排序,以确保排序一致

select id::text
from table
order by id
union
select name::text
from table
order by id
union
select url::text
from table
order by id
最简单的方法是

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $ids[] = $row['id'];
    $names[] = $row['names'];
    $wwws[] = $row['website'];
}
然后迭代这三个数组。当然有更好的方法可以做到这一点

最简单的方法是

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $ids[] = $row['id'];
    $names[] = $row['names'];
    $wwws[] = $row['website'];
}

然后迭代这三个数组。当然有更好的方法可以做到这一点

虽然不是疯狂的优雅,但这确实有效,而且非常简单。非常感谢。虽然不是疯狂的优雅,但这确实有效,而且非常简单。非常感谢。