2016-06-21 33 views
1

我写迭代的PHP文件通过我的所有服务器上的数据库,并取代条目的整列(或两个)四****保护敏感信息的迭代。不过,我有下面的代码在SQL语法返回一个解析错误,我现在用:语法错误在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); 
?> 

这是错误消息我收到回:

INFORMATION_SCHEMA

无法选择数据库:你的SQL语法有错误;检查对应于你的MySQL服务器版本的在线使用近“”正确的语法手册1

我觉得这个错误发生,因为当我通过我的数据库循环作为一个数组,由于某种原因,之后的条目是空白的。不完全确定这是为什么。当我在Sequel Pro中尝试SHOW DATABASES;查询时,它会返回正确的数据库列表。这是列表的例子:

  1. INFORMATION_SCHEMA
  2. MySQL的
  3. performance_schema
  4. DB1
  5. DB2
  6. DB3
  7. 等....

我PHP解释器和MySQL服务器版本sion都是5.6

+0

您对'mysqli_fetch_array()'的单个调用只返回第一行。你应该把这个像一个正常的查询和调用'mysqli_fetch_array()''一环while'而不是用'计数的'for'循环内()'。 –

+0

你的意思'而($ d = mysqli_fetch_array($水库))'? – Jodo1992

+1

是,然后'$分贝= $ d [ '数据库'];'因为'SHOW DATABASES'查询返回称为'Database'单个列。它的行为与普通的'SELECT'语句相同,返回1列和N行。 –

回答

1

MySQL将特殊查询SHOW DATABASES与查询的常规SELECT基本相同的方式发送给查询客户端,因此您的应用程序代码可以完全按照您处理的方式处理它一个常规的SELECT声明。

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

> show databases; 
+-------------------------+ 
| Database    | 
+-------------------------+ 
| information_schema  | 
| db1      | 
| db2      | 
+-------------------------+ 
3 rows in set (0.00 sec) 

因此而不是for循环利用count()mysqli_fetch_array()单一的通话,使用你会在SELECT查询中使用相同的while循环结构,并用它分配$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... 
}