M
M
McThinker2019-10-22 10:49:59
PHP
McThinker, 2019-10-22 10:49:59

How to display data from 2 databases in parallel?

I'm not strong in php, I just understand, please tell me how to display the data correctly:
here is the controller

<?php
class DBController {
  private $host = "localhost";
  private $user = "root";
  private $password = "";
  private $database = "search_airports";
  private $databaseReg = "orders";
  private $conn;
  private $connReg;

  function __construct() {
    $this->conn = $this->connectDB();
        $this->connReg = $this->connectDB();
  }

  function connectDB() {
    $conn = mysqli_connect($this->host,$this->user,$this->password,$this->database);
    return $conn;
  }
    function connectDBReg() {
        $connReg = mysqli_connect($this->host,$this->user,$this->password,$this->databaseReg);
        return $connReg;
    }

  function runQuery($query) {
    $result = mysqli_query($this->conn,$query);
    while($row=mysqli_fetch_assoc($result)) {
      $resultset[] = $row;
    }
    if(!empty($resultset))
      return $resultset;
  }
    function runReqlament($reqlament) {
        $result2 = mysqli_query($this->connReg,$reqlament);
        return $result2;
    }
  function numRows($query) {
    $result  = mysqli_query($this->conn,$query);
    $rowcount = mysqli_num_rows($result);
    return $rowcount;	
  }
    function numRowsReg($reqlament) {
        $result2  = mysqli_query($this->connReg,$reqlament);
        $rowcount = mysqli_num_rows($result2);
        return $rowcount;
    }
}
?>

Here is the output:
<?php
require_once("dbcontroller.php");
$db_handle = new DBController();
$reqlament = "SELECT id, servicename, Reglament FROM cs_service WHERE id";

if(!empty($_POST["keyword"])) {
    $query ="SELECT DISTINCT(city_name), country_name, highway FROM country WHERE city_name like '" . $_POST["keyword"] . "%' ORDER BY city_name LIMIT 0,5";

    $result = $db_handle->runQuery($query);
    $result2 = $db_handle->runReqlament($reqlament);
    if(!empty($result)) { ?>
        <ul id="country-list">
            <?php
            foreach($result as $country) {
                ?>
                <li onClick="selectCountry('<?php echo $country["city_name"]; ?> (<?php echo $country["country_name"]; ?>) <?php
                     if($country["highway"] == 1) echo "- внутренний рейс";
                elseif ($country["highway"] == 2) echo "- средняя магистраль";
                elseif ($country["highway"] == 3) echo "- дальняя магистраль";
                elseif ($country["highway"] == 4) echo "- экзотическое направление";?>'); "><?php echo $country["city_name"]; ?> (<?php echo $country["country_name"]; ?>)</li>
            <?php } ?>
        </ul>
        <?php
    }
    if(!empty($result2)) { ?>
        <p id="reglament">
            <?php
            foreach($result2 as $cs_service) {
                ?>
                <span><?php echo $cs_service["servicename"]; ?></span>
            <?php } ?>
        </p>
        <?php
    }
}
?>

Instead of "- internal flight" I need to pull data from $reqlament, but the problem is that I don't know how to correctly do foreach in foreach. In general, please advise

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
ThunderCat, 2019-10-22
@McThinker

1) Do not call a controller something that is not a controller.
2) If you have 2 db - you don't need to initialize them in one object, you need to create 2 objects (1 per db) with your own settings.
3) WHERE id - just a smart condition.
4) $_POST, $_GET, and in general, raw data coming from the frontend cannot be inserted into requests. Firstly, they need to be at least basicly validated, and secondly, there are prepared statements for this.
5) Than in general division of the data into 2 bases is caused? It seems that you are doing something wrong initially, at the data structure design level.

E
Egor T, 2019-10-22
@Nengchak

spoiler
if($country["highway"] == 1) {
    foreach($result2 as $req) {
     //...
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question