3

这是我当前的代码

function getUserDetails($username=NULL, $id=NULL) {
    if($username!=NULL) {
        $column = "user_name";
        $data = $username;
    }
    elseif($id!=NULL) {
        $column = "id";
        $data = $id;
    }

    global $db;

    $query = $db->prepare("SELECT id, username, permissions, forename, surname, password, email, courseid, choiceid, lastlogin, active FROM users WHERE $column = :column");
    $query->bindParam(":column", $data);
    $query->execute();
    $query->bind_result ($id, $username, $permissions, $forename, $surname, $password, $email, $courseid, $choiceid, $lastlogin, $active);
    while ($query->fetch()){
        $row = array('id' => $id, 'userlevel' => $permissions, 'username' => $username, 'forename' => $forename, 'surname' => $surname, 'password' => $password, 'email' => $email, 'courseId' => $courseid, 'choiceId' => $choiceId, 'lastlogin' => $lastlogin, 'active'=> $active);
    }
    return ($row);
}

我一直在尝试将其转换为 pdo,因为我发现 bind_result 不适用于 pdo - 谁能帮助我知道我应该做什么?

我已经读过我应该使用 fetch?但我真的很困惑。

[编辑]

我试过这个:

function getUserDetails($username=NULL,$id=NULL) {
    if($username!=NULL) {
        $column = "user_name";
        $data = $username;
    }
    elseif($id!=NULL) {
        $column = "id";
        $data = $id;
    }

    global $db;

    $query = $db->prepare("SELECT id, username, permissions, forename, surname, password, email, courseid, choiceid, lastlogin, active FROM users WHERE $column = :column");
    $query->bindParam(":column", $data);
    $query->execute();

    $results = array();
    while ($row = $query->fetch(PDO::FETCH_ASSOC)) { 
        $results[] = $row;
    }

    return ($results);
}

这是朝着正确方向迈出的一步吗?

[编辑2]

将我的代码更新为:

function getUserDetails($username) {
    global $db;

    $query = $db->prepare("SELECT * FROM users WHERE username = :username");
    $query->bindParam(":username", $username);
    return $query->fetch(PDO::FETCH_ASSOC);
}

$username = 'uname';
$result = getUserDetails($username);
print_r($result);

但是它什么也没打印。用户名肯定存在。


我尝试了一个带有一些虚拟数据的测试数据库

$data = '2';
$sth = $db->prepare("SELECT * FROM test WHERE id = :id");
$sth->bindParam(":id", $data);
$sth->execute();
$result = $sth->fetch(PDO::FETCH_ASSOC);
print_r($result);   

我试图弄清楚我如何访问打印数组中的内容:数组的输出为

Array ( [Id] => 2 [Name] => tom ) 

我(例如)怎么做

$name = $result['name'];    //line 67

当我尝试该代码时,我得到

Notice: Undefined index: name in <directory>\test.php on line 67

弄清楚了!

function getUserDetails($username) {
    global $db;

    $sth = $db->prepare("SELECT id, username, permissions, forename, surname, password, email, courseid, choiceid, lastlogin, active FROM users WHERE username = :username");
    $sth->bindParam(":username", $username);
    $sth->execute();
    $result = $sth->fetch(PDO::FETCH_ASSOC);

    return $result;
}


$username = 'un';
$userdetails = getUserDetails($username);
echo $userdetails['forename'];

它给了我正确的答案!感谢您的帮助

4

1 回答 1

3

是的!
这是朝着正确方向迈出的一大步。
如您所见, mysqli在绑定占位符返回结果时绝对不能用于准备好的语句。 而 PDO 可以使用更少的代码解决您的问题。 您根本不需要与 PDO 进行无用的绑定 - 只需使用 fetchAll() 获取所有结果:

function getUserDetails($username=NULL,$id=NULL) {
    if ($username) {
        $column = "user_name";
        $data = $username;
    } elseif($id) {
        $column = "id";
        $data = $id;
    } else {
        return;
    }
    global $db;

    $query = $db->prepare("SELECT * FROM users WHERE $column = ?");
    $query->execute(array($data));
    return $query->fetchAll();
}

可是等等。如果它是用户详细信息,为什么要返回一个数组?它只会向返回的数组添加无用的维度。对于这种情况,让它

    return $query->fetch();

而不是 fetchAll()。
但是你需要很多行 - 使用后一种方法。
关于其他方法和有用的连接选项,请参阅标签 wiki

于 2013-03-04T05:14:25.000 回答