【问题标题】:Passing a returned string into another string and using it in an if statement [closed]将返回的字符串传递给另一个字符串并在 if 语句中使用它[关闭]
【发布时间】:2012-12-07 00:49:30
【问题描述】:

我有这段代码:

<?php
            require_once 'Membership.php';
    $membership = New Membership();
    $AccountType = $membership->is_Account($currUser);
    if ($AccountType == 'S') {
        echo '<li>';
        echo $AccountType;
        echo '</li>';
    }
   ?>

它调用了一个不同的文档membership.php,其函数执行此操作:

function is_Account($currUser){
    //Find out if user is a buyer or a seller
    $mysql = New Mysql();
    $mysql->get_Account($currUser); 
}

出于组织原因 - 转到执行此操作的文档(此位有效,因为其他功能正常工作):

function get_Account($currUser) {
    $mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME);
    if (!$mysqli) {
        die('There was a problem connecting to the database.');
    }       
    $queryAcct = "SELECT Id
            FROM Users
            WHERE Username = '$currUser'
            LIMIT 1";
    if ($Result = $mysqli->query($queryAcct)){
        if (!$Result) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }
        else {
            $row = $Result->fetch_assoc();
            return $row["Id"];
        }
    }
    $mysqli->close();
}

所以 - 最后一个函数有问题。我想使用返回的 Id,例如,如果它是 1,我会显示与 Id 1 相关的内容(多人将拥有该 Id 编号,因为它是一个层级系统)。

基本上 - 我不知道为什么它不将 Id 返回到原始代码位。我假设我做错了什么。我只是不知道如何解决它。

回显 $row["Id"] 会起作用,所以我知道那一点很好,它不是 sql 问题。但这不是我想做的。

【问题讨论】:

标签: php sql function return


【解决方案1】:

您需要在 is_Account() 函数中返回来自 get_Account() 的值:

return $mysql->get_Account($currUser);

【讨论】:

  • 他们喜欢 stackoverflow。我纠结了一个半小时。还在学习!谢谢 :) 它会让我在 6 分钟内选择你的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-25
  • 1970-01-01
  • 2018-12-13
  • 1970-01-01
  • 1970-01-01
  • 2012-11-01
  • 1970-01-01
相关资源
最近更新 更多