0

基本上我有一段代码,它有一个 switch 语句和一些函数。它使用浏览器“”给出的参数进入 switch 语句?step=X,然后选择适当的函数。

我的问题是,即使在每个函数结束时我指定转到下一个 switch 语句,它也不会这样做,并且不知何故它变成了我在浏览器中使用“step = x”选择的函数的无限循环......

为什么卡在 1 个函数中而不是遍历它们?(该步骤的代码在脚本末尾突出显示)

我在任何选定函数的浏览器中得到输出。所以它进入了开关和选定的函数......但随后它变成了无限循环,因为在浏览器中我得到了 300 个相同的回显语句。它永远无法遍历它们或退出选定的函数。

<?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;
    }


}
?>
4

1 回答 1

1

使固定

function stepFunction ($step,$fOT){
global $db,$IDB1,$IDB2,$step,$fOT;

function stepFunction ($step){
global $db,$IDB1,$IDB2;

您正在$step使用同名的全局变量覆盖您的变量。因此,当你和?step=0你一起打电话stepFunction时,0它会转到createTablePagesthen 它会转到stepFunctionwith 1,但会立即被替换为 0 (因为那是 global$step的值)并且一次又一次地......

于 2012-08-29T09:53:49.080 回答