我写迭代的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;
查询时,它会返回正确的数据库列表。这是列表的例子:
- INFORMATION_SCHEMA
- MySQL的
- performance_schema
- DB1
- DB2
- DB3
- 等....
我PHP解释器和MySQL服务器版本sion都是5.6
您对'mysqli_fetch_array()'的单个调用只返回第一行。你应该把这个像一个正常的查询和调用'mysqli_fetch_array()''一环while'而不是用'计数的'for'循环内()'。 –
你的意思'而($ d = mysqli_fetch_array($水库))'? – Jodo1992
是,然后'$分贝= $ d [ '数据库'];'因为'SHOW DATABASES'查询返回称为'Database'单个列。它的行为与普通的'SELECT'语句相同,返回1列和N行。 –