-2

我需要在函数外使用“问题”变量:

public  function name(){
    if($_SESSION[agT] == "random" AND $_SESSION[ct]=="random"){
    $consult = DB::getConn()->prepare('SELECT * FROM table ORDER BY RAND() LIMIT 1');
    $consult->execute();
    $question=$consult->fetch(PDO::FETCH_ASSOC);
}

喜欢

echo "$question[column];
echo "$question[anothercolumn]; 
4

2 回答 2

0

return $question

然后调用这个函数

$val = name();

于 2013-06-09T15:07:02.620 回答
0

正如其他人所说,您应该学习过程和函数编程的基础知识。您可以从函数返回查询的值:

public  function_name(){
    if($_SESSION[agT] == "random" AND $_SESSION[ct]=="random"){
    $consult = DB::getConn()->prepare('SELECT * FROM table ORDER BY RAND() LIMIT 1');
    $consult->execute();
    $question=$consult->fetch(PDO::FETCH_ASSOC);

    return $question
}

$result = function_name();

echo $question['col1'];
echo $question['col2']; 
于 2013-06-09T15:22:21.607 回答