PHP警告:为foreach()提供的参数无效,伪造数据库无法在wordpress中工作
我不知道为什么我从wordpress收到这个警告,它在本地wamp设置上运行良好,它应该是一个假的数据库。但它给出了foreach()错误警告。只是为了训练PHP警告:为foreach()提供的参数无效,伪造数据库无法在wordpress中工作,php,wordpress,syntax,foreach,Php,Wordpress,Syntax,Foreach,我不知道为什么我从wordpress收到这个警告,它在本地wamp设置上运行良好,它应该是一个假的数据库。但它给出了foreach()错误警告。只是为了训练 <?php // Fake database functions // For demonstration purposes, it is not worth setting up a whole // database. So these functions will fake it by putting a // data
<?php
// Fake database functions
// For demonstration purposes, it is not worth setting up a
whole
// database. So these functions will fake it by putting a
// database-like associative array in the session.
// Obviously, in real life, you would use a database and
remove
// this file.
**function initialize_fake_database()** {
if(!isset($_SESSION['fake_database'])) {
$users = [
[
'id' => 0,
'username' => 'kskoglund',
'hashed_password' =>
password_hash('secret', PASSWORD_BCRYPT)
], [
'id' => 1,
'username' => 'jsmith',
'hashed_password' =>
password_hash('Never73#Guess', PASSWORD_BCRYPT)
], [
'id' => 2,
'username' => 'ljohnson',
'hashed_password' =>
password_hash('Not+A+Password', PASSWORD_BCRYPT)
], [
'id' => 3,
'username' => 'abutcher',
'hashed_password' =>
password_hash('wonderboy', PASSWORD_BCRYPT)
]
];
$blacklisted_ips = [
['ip' => '5.5.5.5'],
['ip' => '6.6.6.6'],
['ip' => '7.7.7.7']
];
// There are 3 fake tables in our fake
database.
$_SESSION['fake_database'] = [
'users' => $users,
'failed_logins' => [],
'blacklisted_ips' => $blacklisted_ips
];
}
}
function remove_fake_database() {
$_SESSION['fake_database'] = null;
}
// Search our fake database $table for all records
// where the specified $key has the given $value.
// Returns an array, even if only one record is found.
function find_all_in_fake_db($table, $key, $value) {
$fake_db = $_SESSION['fake_database'];
$fake_table = $fake_db[$table];
$results = [];
**foreach($fake_table as $record) {
if (isset($record[$key]) && $record[$key] == $value) {
// This is a matching record, add it *
to results array
$results[] = $record;*
}
}
return $results;
}
// Returns first matching record or null
function find_one_in_fake_db($table, $key, $value) {
$results = find_all_in_fake_db($table, $key, $value);
$result = count($results) > 0 ? $results[0] : null;
return $result;
}
// Add a new record to the specified fake table
function add_record_to_fake_db($table, $record) {
$fake_db = $_SESSION['fake_database'];
$fake_table = $fake_db[$table];
$fake_table[] = $record;
// replace old data with updated versions
$fake_db[$table] = $fake_table;
$_SESSION['fake_database'] = $fake_db;
return true;
}
// Update an existing record in fake table
// You must specify the key used to identify the record
// to be updated.
function update_record_in_fake_db($table, $key, $new_record) {
$fake_db = $_SESSION['fake_database'];
$fake_table = $fake_db[$table];
$value = $new_record[$key];
// use a reference (&) so that the update happens to
// the record in the table.
foreach($fake_table as &$record) {
if (isset($record[$key]) && $record[$key] == $value) {
// This is the record to update
$record = array_merge($record,
$new_record);
}
}
// replace old data with updated versions
$fake_db[$table] = $fake_table;
$_SESSION['fake_database'] = $fake_db;
return true;
}
?>
这修复了foreach错误,然后我使用了旧版本的php,出现了密码哈希问题,并用于修复它
if(!empty($fake_table)){ foreach($fake_table as $record) { if (isset($record[$key]) && $record[$key] == $value) { /* bla bla bla */} } }else{ return false; }
// This is a matching record, add it to results array
$results[] = $record;
}
return $results;
仔细检查$fake_table是否有值并且是数组。发布错误和调用initialize_fake_database()的行?这是错误开始的行,错误是foreach($record作为$record)的每个无效参数($fake_table){if(isset($record[$key])&&$record[$key]=$value){通常,foreach函数显示错误警告如果数组参数为空,则没有数组可以循环。因此,您可以使用checked if empty进行预测。
if(!empty($fake_table)){foreach($fake_table as$record){if(isset($record[$key])&$record[$key]=$value){/*bla bla bla bla bla bla bla*/}}否则{return false;}