Php 在数据库中迭代时SQL查询中出现语法错误

Php 在数据库中迭代时SQL查询中出现语法错误,php,mysql,sql,database,Php,Mysql,Sql,Database,我正在编写一个PHP文件,该文件在服务器上遍历我的所有数据库,并用四个**替换一整列(或两列)条目以保护敏感信息。但是,下面的代码在我使用的SQL语法中返回了一个解析错误: <?php /** * This replaces an entire column within a table with a 4 asterisk long string */ $host = 'example.example.com'; $user = 'example'; $password = 'pas

我正在编写一个PHP文件,该文件在服务器上遍历我的所有数据库,并用四个
**
替换一整列(或两列)条目以保护敏感信息。但是,下面的代码在我使用的SQL语法中返回了一个解析错误:

<?php
/**
 * This replaces an entire column within a table with a 4 asterisk long string
 */

$host = 'example.example.com';
$user = 'example';
$password = 'password';

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

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

// Get the databases as an array
$res = mysqli_query($connection, "SHOW DATABASES");
$d = mysqli_fetch_array($res);

// Loop through the array of databases
for ($i = 0; $i < count($d); $i++){

    $db = $d[$i];
    echo "$db\n";

    // To skip the first database information_schema
    if ($i > 0){
        $sql1 = /** @lang text */
                "USE $db";
        $query1 = mysqli_query($connection, $sql1);

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

        $sql2 = /** @lang text */
                "SELECT * FROM `info`";

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

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

        while ($row = mysqli_fetch_array($query2)){

            $id = $row['id'];

            $sql3 = /** @lang text */
                "IF COL_LENGTH('info','borrower') IS NOT NULL
                 BEGIN
                    UPDATE `info`
                        SET `borrower` = '****'
                        WHERE `id` = '$id'
                 END";

        $query3 = mysqli_query($connection, $sql3);

        if (!$query3){
            die('Could not replace number with "****" '.mysqli_error($connection));
        }

        $sql4 = /** @lang text */
                "IF COL_LENGTH('info','coborrower') IS NOT NULL
                 BEGIN
                    UPDATE `info`
                        SET `coborrower` = '****'
                        WHERE `id` = '$id'
                 END";

        $query4 = mysqli_query($connection, $sql4);

        if (!$query4){
            die('Could not replace number with "****" '.mysqli_error($connection));
        }
    }
}

mysqli_close($connection);
?>

MySQL将特殊查询
SHOW DATABASES
与常规的
SELECT
查询发送到查询客户机的方式基本相同,因此,应用程序代码可以与处理常规
SELECT
语句的方式完全相同

showdatabases
查询返回一个名为
Database
的列和每个数据库的一行

> show databases;
+-------------------------+
| Database                |
+-------------------------+
| information_schema      |
| db1                     |
| db2                     |
+-------------------------+
3 rows in set (0.00 sec)
因此,与使用
count()
和单个调用
mysqli\u fetch\u array()
for
循环不同,在
选择
查询中使用相同的
循环结构,并使用它分配
$db

$res = mysqli_query($connection, "SHOW DATABASES");
if (!$res) {
  // handle error...
}
// On query success, fetch in a normal loop 
while ($d = mysqli_fetch_assoc($res)) {
  // Database name is in the column `Database`
  $db = $d['Database'];

  // Advisable to quote it with backticks...
  $sql1 = "USE `$db`";
  // etc...
}

mysqli\u fetch\u array()
的单个调用只返回第一行。你应该把这当作一个普通的查询,在
while
循环中调用
mysqli\u fetch\u array()
,而不是使用
count()调用
for
循环。你的意思是
while($d=mysqli\u fetch\u array($res))
?是的,然后
$db=$d['Database']
因为
SHOW DATABASES
查询返回一个名为
Database
的列。它的行为与正常的
SELECT
语句相同,返回1列和N行。就是这样!非常感谢。你能发布一个正式的答案吗?这样我就可以把它标记为正确和完整了。当然,我会写下来的。