PHP函数返回不起作用

时间:2015-12-18 01:48:14

标签: php function return

我有一个问题。我写这个函数tu从我的数据库实体恢复一个前一个分支,我希望该函数返回一个字符串,我可以看到这个信息。这是代码:

function stampaOggettoCollezione($tipoOggetto, $id, $connection, $percorso="")
{
    $i=0;

    switch($tipoOggetto)
    {
        case 'casaeditrice':
            return $percorso;
            break;

        case 'storia':
            $dati = array("storia", "IDCasaEditrice", "casaeditrice");
            break;

        case 'testata':
            $dati = array("testata", "IDStoria", "storia");
            break;

        case 'albo':
            $dati = array("albo", "IDTestata", "testata");
            break;
    }

            $sql = "SELECT * FROM ".$dati[0]." WHERE ID=$id";
            $result=mysqli_query($connection,$sql);
            $IDSuperiore=mysqli_fetch_array($result)[$dati[1]];

            $sql = "SELECT * FROM ".$dati[2]." WHERE ID=$IDSuperiore";
            $result = mysqli_query($connection, $sql);
            $nomeSuperiore=mysqli_fetch_array($result)['nome'];

            $percorso = $percorso . $nomeSuperiore;

            stampaOggettoCollezione($dati[2], $IDSuperiore, $connection, $percorso);
}

我确信代码有效,我在代码中尝试使用echo来检查最终路径。我的问题是我不能像我的网页中的字符串那样写回报。你有什么想法?声明存在一些问题?感谢

2 个答案:

答案 0 :(得分:1)

我不确定我理解,因为英语很难理解,但似乎所有你需要做的就是写:

return $result;

在函数的末尾,或者您想要返回的任何字符串变量。然后在函数后使用echo stampaOggettoCollezione(arguments...)来回显此值。

答案 1 :(得分:1)

替换

public static final

select count(age) + (select count(age) 
    from Table2 where age > @age) 
from Table1 where age > @age

您必须将stampaOggettoCollezione($dati[2], $IDSuperiore, $connection, $percorso); return stampaOggettoCollezione($dati[2], $IDSuperiore, $connection, $percorso); 的所有电话中一直传回原始电话。阅读有关互联网递归的更多信息。

或者,如果您更改函数以构建连接表的查询,我相信您可以实现相同的效果。它将消除递归的需要,减少数据库调用的数量,并且除了函数本身之外不应该改变任何东西。

// dynamically queries more tables based on what the inital table to query is
function stampaOggettoCollezione($tipoOggetto, $id, $connection, $percorso="")
{
    $selectQuery = "SELECT ";
    $columnsToSelect = "";
    $fromQuery = "";
    $whereClause = " WHERE ";

    switch($tipoOggetto) {
        case 'albo':
            $columnsToSelect .= ", a.nome AS aNome";
            $fromQuery = " LEFT JOIN albo a ON t.ID = a.IDTestata";
        case 'testata':
            $columnsToSelect .= ", t.nome AS tNome";
            $fromQuery = " LEFT JOIN testata t ON s.ID = t.IDStoria" . $fromQuery;
        case 'storia':
            $columnsToSelect .= ", s.nome AS sNome";
            $fromQuery = " LEFT JOIN storia s ON ce.ID = s.IDCasaEditrice" . $fromQuery;
        case 'casaeditrice':
            $columnsToSelect .= ", ce.nome AS ceNome";
            $fromQuery = " FROM casaeditrice ce" . $fromQuery;
            break;
    }
    // remove the first `, `
    $selectQuery .= substr($columnsToSelect, 2);

    switch($tipoOggetto)
    {
        case 'casaeditrice':
            $whereClause .= "c.ID = $id";
            break;
        case 'storia':
            $whereClause .= "s.ID = $id";
            break;
        case 'testata':
            $whereClause .= "t.ID = $id";
            break;
        case 'albo':
            $whereClause .= "a.ID = $id";
            break;
    }

    $qry = $selectQuery . $fromQuery . $whereClause;

    $result=mysqli_query($connection,$sql);
    $nomeSuperiore= "";
    $nomes = mysqli_fetch_array($result);
    // might take some work to get the output in the right order
    foreach($nomes as $nome)
        $nomeSuperiore .= $nome;

    return $nomeSuperiore;
}