Automatic PDO and MYSQL listing

Asked

Viewed 78 times

1

Good afternoon. I am trying to make the tables be automatically listed, without me having to name the field. But it’s showing errors.

$conn = new PDO($dsn, $username, $password, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''));
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$result = $conn->query("SHOW TABLES");

                while ($row = $result->fetch(PDO::FETCH_NUM)){

                    $table = $conn->query("SHOW FULL COLUMNS FROM ".$row[0]);
                    $busca = $conn->query("SELECT * FROM ".$row[0]);
                    $rows = '';
                    echo '<br>';

                    while ($res = $table->fetch(PDO::FETCH_NUM)){
                        echo $res[0].'<br>';    

                        while ($resS = $busca->fetch(PDO::FETCH_ASSOC)){
                            $rows = $res[0] .' - '. $resS[$res[0]].'<br>';
                            echo $rows;
                        }           
                    }               

                }

$res[0] is listing the name of the fields correctly:

idCliente
cliente_Nome
cliente_Cpf
cliente_Endereco
cliente_Numero
cliente_Bairro
cliente_Cidade
cliente_Estado

But when I try to use the data of each table, returned by $ress, it does not present the entered values, or when it shows only the id or duplicates.

1 answer

1


Jose, I managed to do something here. From what I understand, I missed going through the data in $ress. Look at the code I made below and see if it meets you:

$result = $conn->query("SHOW TABLES");

while ($row = $result->fetch(PDO::FETCH_NUM)){
    $table = $conn->query("SHOW FULL COLUMNS FROM " . $row[0]);
    $busca = $conn->query("SELECT * FROM " . $row[0]);
    $rows = '';

    while($res = $table->fetch(PDO::FETCH_NUM)){
       while ($resS = $busca->fetch(PDO::FETCH_ASSOC)){
          foreach ($resS as $chv => $vlr){
             echo $chv . ' - ' . $vlr . '<br>';
          }
       }
    }
}
  • 1

    It worked out buddy. All I’m doing is to try to check two databases and after the comparison do a synchronization. You have a basis beyond that for me to follow?

  • Look, if I were to do something similar, I would read the records of a bank and pick up some key fields to see if these fields exist in the other bank. For example in a customer register, I would take the CPF field to make this comparison. If it doesn’t exist, write there. I believe someone with more experience has been through it.

  • Just one thing, Luis, if the solution has served you, please mark the answer as Resolved. Thank you!

  • 1

    I will try, because there is a client who wants my system both local and web. Dai I could not find any library that does this synchronization. So kind of desperate. I’ve tried mysqldump, but it didn’t work. If you know something, pff. I would be eternally grateful!! Vlw right now.

  • Open a question about it here, Luis. There are a lot of experienced people who can probably help you. Database synchronization is an interesting thing. However, would it be more appropriate after the end of synchronization for both systems to access only one bank?

  • 1

    Yes, I had done so, the two accessed the web, but the client is afraid that if missing internet and such.

  • I get it... a suggestion (I’m a layman...) that I give would be to every local recording and on the web, if you have a problem recording the web creates some "flag", something saying that the synchronization should be done and then leave a service to be run at dawn, for example, to perform this synchronization if the "flag" is created. Then I would be ensuring that whenever there is a problem, at a certain moment the synchronization will enter.

  • Okay. Thank you. Let’s try.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.