Jump to content


Photo

converting mysqli to pdo - bind_result

pdo

Best Answer AyKay47, 03 March 2013 - 08:55 PM

Example#1 in the PHP manual PDOStatement::bindColumn replicates your logic using PDO::FETCH_BOUND mode.

Go to the full post


  • Please log in to reply
2 replies to this topic

#1 goldfishdancer

goldfishdancer

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 03 March 2013 - 08:19 PM

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.



#2 teynon

teynon

    Advanced Member

  • Members
  • PipPipPip
  • 895 posts

Posted 03 March 2013 - 08:27 PM

Try $query->execute(array("column" => $data)); without the bindParam


Support my Kickstarter Project!
http://www.kickstart...7618755/antroid

http://www.thomaseynon.com

Vulnerabilities: http://cwe.mitre.org...x.html#Guidance - MySQL.com hacked with SQL Injection - If it happened to them, it can happen to you.


#3 AyKay47

AyKay47

    Sick!

  • Members
  • PipPipPip
  • 3,287 posts
  • LocationEast Coast, U.S.
  • Age:24

Posted 03 March 2013 - 08:55 PM   Best Answer

Example#1 in the PHP manual PDOStatement::bindColumn replicates your logic using PDO::FETCH_BOUND mode.


Hola!
I'm not going to hold your hand and write the code for you - ain't nobody got time for that!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com