在函数外部使用查询值

时间:2013-06-09 15:03:49

标签: php variables pdo

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

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]; 

2 个答案:

答案 0 :(得分:0)

DO return $question

然后调用此函数

$val = name();

答案 1 :(得分: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'];