0

我正在尝试在 3 个相邻<div>容器中显示 3 个单独查询的结果。三个查询如下:

$query="SELECT * FROM students WHERE WeekOne='employer'";
$query="SELECT * FROM students WHERE WeekTwo='employer'";
$query="SELECT * FROM students WHERE WeekThree='employer'";

期望的输出:

第一周 ----- 第二周 ----- 第三周

结果 1 ---------- 结果 1 ------------ 结果 1

结果 2 ---------- 结果 2 ----------- 结果 2 (等等...)

有人可以帮我吗?我已经看过 PHP 手册上的 multi_query,但不知道如何实现它。非常感谢。

@Emmanuel G

<?

$user = "xx";
$user_password = "xx";
$db_name = "xx";
$db = new mysqli ("xx", $user, $user_password, $db_name);

$queries = array(
    "SELECT * FROM students WHERE WeekOne='$Name'",
    "SELECT * FROM students WHERE WeekTwo='$Name'",
    "SELECT * FROM students WHERE WeekThree='$Name'"
);

// iterate through the queries and their results
foreach($queries as $query){
    $result = $mysqli->query($query);
    echo '<div>'; // open that div up
    while($row    = $result->fetch_assoc()){       // gives u an array hoorah!
        echo '<pre>'.print_r($row, true).'</pre>'; // just to look at it
        echo "<p>{$row['FirstName']}</p>";              // just an example
    }
    echo '</div>'; // close it up
}

?>
4

4 回答 4

0

最简单的方法是将查询更改为:

$query="SELECT * FROM students WHERE WeekOne='employer' OR WeekTwo='employer' OR WeekThree='employer'";

然后相应地格式化输出。

于 2012-06-20T12:55:04.453 回答
0

是的,更改您的查询以将所有结果集中到一个查询请求中,然后您从数据结果中进行操作,多次点击以获取服务器的简单答案并不是最佳实践,因为您的查询非常简单。

但是对于你的问题

multi_query($query)  //you can use this

看看这里http://suite101.com/article/how-tor-run-multiple-mysql-queries-with-php-a105672

于 2012-06-20T12:58:11.043 回答
0

要显示它,只需遍历每个单独的结果并将其放入 div 中。为了使它们相邻,您可以在 div 中添加一些 css 以使它们浮动,因为 div 本质上是块元素,并且在没有帮助的情况下不会水平排列。

假设您已经设置了数据库连接并且应该使用 mysqli 或者您可以像这样生成标记:

// u should try to make this one query but that's irrelevant to the question 
// so I'll leave that to you to do later
$queries = array(
    "SELECT * FROM students WHERE WeekOne='employer'",
    "SELECT * FROM students WHERE WeekTwo='employer'",
    "SELECT * FROM students WHERE WeekThree='employer'"
);

// iterate through the queries and their results
foreach($queries as $query){
    if($result = $mysqli->query($query)){// only do stuff if you get a result
        echo '<div>';                                  // open that div up
        while($row = $result->fetch_assoc()){          // gives u an array hoorah!
            echo '<pre>'.print_r($row, true).'</pre>'; // just to look at it
            echo "<p>{$row['data']}</p>";              // just an example
        }
        echo '</div>';                                 // close it up

        // mysqli free the result to prep for the next one
        $result->free();
    } else {
        echo "Query failed to return results:\n$query\n";
    }
}
于 2012-06-20T13:06:19.047 回答
-1

我猜你正在将雇主作为 URL 参数传递,所以无论哪种方式,你都可以传递week你想要比较的,比如说

$week = $_GET['weekOne'];
$employer = $_GET['employer'];

$query="SELECT * FROM students WHERE $week= $employer";

在执行查询之前确保你escape的数据,以避免sql注入

我希望它会帮助你。谢谢

于 2012-06-20T13:00:32.367 回答