简体   繁体   中英

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. When looking for particular results within my database, it would sometimes be simpler to join multiple queries utilizing PHP. If for example I wanted to join a secondary query to the below code, what PHP script would be used to do this? One example is I did a query utilizing subqueries with LEFT JOINs as one query. I would like to get the same results as that query only doing it by joining multiple queries through 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.

<?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. To accomplish your needs, you can write a query using a subselect or execute two separate $mysqli->query() statements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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