mardi 21 juin 2016

Looping through MySQL Databases on Single Server in PHP

I'm trying to loop through all of the databases on one server and replace sensitive information with "****" in one table that each of the databases share in PHP. I know I could connect to each database individually and then run the rest of the script, but that would be a painfully long code file as I have over 90 databases on this server. How would I write PHP code that can loop through all of the databases and just have the one action of replacing in all rows? I can do it with a singular database just fine:

<?php
$host = 'example.example.com';
$user = 'example';
$password = 'password';
$db = 'database1';

$connection = new mysqli($host, $user, $password, $db);

if (!$connection){
    die('Could not connec to server: '.mysqli_error($connection));
}

$sql1 = "SELECT * FROM `info`"; // The table
$query1 = mysqli_query($connection, $sql1);

if (!$query1){
    die('Could not select from `info`: '.mysqli_error($connection));
}

while ($row = mysqli_fetch_array($query1)){
    $id = $row['id'];

    $sql2 = "UPDATE `info`
                 SET `sensitiveinfo1` = '****', `sensitiveinfo2` = '****'
                 WHERE `id` = '$id'";

    $query2 = mysqli_query($connection, $sql2);

    if (!$query2){
        die('Could not replace info with "****": '.mysqli_error($connection));
    }
}
?>

So how would I change this to run through all of the databases instead of just one?

Aucun commentaire:

Enregistrer un commentaire