基本上我有一段代码,它有一个switch语句和一些函数。它使用浏览器"?step=X
"给出的参数进入switch语句,然后选择适当的函数。
我的问题是,即使在我指定转到下一个switch语句的每个函数结束时,它也从未执行过,而且不知何故,它变成了我在浏览器中用"step=x"选择的函数的无限循环。。。
为什么被困在1个函数中而不迭代它们?(该步骤的代码在脚本末尾突出显示)
我在浏览器中获得任何选定函数的输出。因此它进入开关和所选功能。。。但后来它变成了一个无限循环,因为在broswer中我得到了300个相同的echo语句。它永远无法遍历它们或退出所选函数。
<?php
//DB Config File
$dbFile = 'dbconfig.php';
$username = $_GET['username'];
$password = $_GET['password'];
$server = $_GET['server'];
$dbname = $_GET['dbname'];
$step = $_GET["step"];
function createfile ($dbFile) {
//Creates File and populates it.
$fOpen = fopen($dbFile, 'w');
global $username, $password, $server, $dbname;
$fString .= "<?php'n";
$fString .= "// Database Constants'n";
$fString .= "'$DB_SERVER =" . "'"" . $server . "'";'n";
$fString .= "'$DB_USER =" . "'"" . $username . "'";'n";
$fString .= "'$DB_PASS =" . "'"" . $password . "'";'n";
$fString .= "'$DB_NAME =". "'"" . $dbname . "'";'n";
$fString .= "?>";
fwrite($fOpen, $fString);
fclose($fOpen);
return true;
}
try {
$db = new PDO ('mysql:host=' .$server.';dbname='.$dbname,$username,$password);
if ($db) { //if succesful at connecting to the DB
if (file_exists($dbFile)){
if (is_readable($dbFile) && is_writable($dbFile)){
//Creates File, populates it and redirects the user
if (createfile($dbFile)) {
echo "nelxt";
stepFunction($step);
exit ();
}
} else {
echo "The file {$dbFile} cannot be accessed. Please configure the file manualy or grant Write and Read permission."; }
} else {
//Creates File, populates it and redirects the user
if (createfile($dbFile)) {
echo "next";
stepFunction($step);
exit ();
}
}
}
} catch (PDOException $e) { //Catchs error if can't connect to the db.
echo 'Connection failed: ' . $e->getMessage();
}
// Prepare SQL Statements
$IDB = $db->prepare(
"CREATE TABLE pages (
id int(11) NOT NULL auto_increment,
subject_id int(11) NOT NULL,
menu_name varchar(30) NOT NULL,
position int(3) NOT NULL,
visible tinyint(1) NOT NULL,
content text NOT NULL,
PRIMARY KEY (id)
)ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8");
$IDB2 = $db->prepare("
CREATE TABLE subjects (
id int(11) NOT NULL auto_increment,
menu_name varchar(30) NOT NULL,
position int(3) NOT NULL,
visible tinyint(1) NOT NULL,
PRIMARY KEY (id)
)ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8");
$IDB3 = $db->prepare("
CREATE TABLE users (
id int(11) NOT NULL auto_increment,
username varchar(50) NOT NULL,
hashed_password varchar(40) NOT NULL,
PRIMARY KEY (id)
)ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8");
//Set Option to True or False
if (empty ($_GET['fot']) ) {
$fOT = false;
} else { $fOT = true;
}
///////////////////////////////
// PROBLEMATIC STEP BEGINS HERE
///////////////////////////////
function createTablePages (){
global $db,$IDB;
echo "0 <br>";
stepFunction (1);
}
function createTableSubjects ($fOT){
global $db,$IDB2;
echo "1 <br>";
stepFunction (2);
}
function createTableUsers ($fOT){
global $db,$IDB3;
echo "3 <br>";
}
function stepFunction ($step,$fOT){
global $db,$IDB1,$IDB2,$step,$fOT;
switch ($step) {
case 0: echo "hola";
createTablePages ($fOT);
break;
case 1: echo "hola2";
createTableSubjects($fOT);
break;
case 2: createTableUsers ($fOT);
break;
}
}
?>
修复
function stepFunction ($step,$fOT){
global $db,$IDB1,$IDB2,$step,$fOT;
至
function stepFunction ($step){
global $db,$IDB1,$IDB2;
您正在使用同名的全局变量覆盖$step
变量。因此,当你使用?step=0
时,你用0
调用stepFunction
,然后它转到createTablePages
,然后用1
转到stepFunction
,但立即被0替换(因为这就是全局$step
的值),一次又一次。。。