简体   繁体   English

将两个MySQL查询与PHP结合

[英]Combining Two MySQL Queries with PHP

The below code is simple PHP/MySQL script that returns the results of a single MySQL query utilizing PHP. 以下代码是简单的PHP / MySQL脚本,该脚本返回使用PHP的单个MySQL查询的结果。 When looking for particular results within my database, it would sometimes be simpler to join multiple queries utilizing PHP. 在数据库中查找特定结果时,有时使用PHP联接多个查询会更简单。 If for example I wanted to join a secondary query to the below code, what PHP script would be used to do this? 例如,如果我想将辅助查询加入以下代码,那么将使用什么PHP脚本来执行此操作? One example is I did a query utilizing subqueries with LEFT JOINs as one query. 一个示例是我使用带有LEFT JOIN的子查询作为一个查询来进行查询。 I would like to get the same results as that query only doing it by joining multiple queries through PHP. 我只想通过PHP联接多个查询来获得与该查询相同的结果。

I have looked for answers to this, but I don't think I know how to phrase it correctly since I just get results on how to do MySQL JOIN operations. 我一直在寻找答案,但是我不知道如何正确地表达它,因为我只是获得有关如何执行MySQL JOIN操作的结果。

<?php
$mysqli = new mysqli("hostname", "username", "password", "marketing");
$result = $mysqli->query("
    SELECT INDV_FIRST_NAME, INDV_LAST_NAME
    FROM INDV_INDIVIDUAL
    LIMIT 100
");
if (!$result) {
    echo $mysqli->error;
    exit;
}
while ($row = $result->fetch_assoc()) {
    echo $row['INDV_FIRST_NAME'] . $row['INDV_LAST_NAME'] . '<br>';
}

Each $mysqli->query() statement will execute only one query. 每个$mysqli->query()语句将仅执行一个查询。 To accomplish your needs, you can write a query using a subselect or execute two separate $mysqli->query() statements. 为了满足您的需求,您可以使用subselect编写查询或执行两个单独的$mysqli->query()语句。

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

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