Jump to content

converting mysqli to pdo - bind_result


goldfishdancer
Go to solution Solved by AyKay47,

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.