Jump to content

converting mysqli to pdo - bind_result


goldfishdancer

Recommended Posts

this is the code with bind_result:

 

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);
}

I've been trying to convert this to work with PDO, as I've found out that PDO doesnt support bind_result. 

 

I've read arround about using fetch assoc but im not entirely sure how to implement it

 

I've tried this:

 

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);
}

 

 

This is a sample of how im trying to use the code

 

	$username = '123';
	$userdetails = getUserDetails($username);
	echo $userdetails['surname'];

 

 

Could anyone please give me a poke in the direction I should be going? I have searched around but I'm just getting more confused.

Link to comment
https://forums.phpfreaks.com/topic/275197-converting-mysqli-to-pdo-bind_result/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.