0

I'm having issues with getting the data from a table through PDO. The following code appears that it should fetch an associative array, but it returns as a boolean.

$pdo = new PDO('mysql:host=localhost;dbname=database', 'username', 'password');
$ps = $pdo->prepare("SELECT * FROM wp_users WHERE user_login = ?");
$ps->execute(array($_GET["user_login"]));
$result = $ps->fetch(PDO::FETCH_ASSOC);
      /*extract($result);*/

var_dump($result);

Can someone help me understand how to return an array of values from the database?

EDIT: Here is the code that actually works. However, I'm trying to break it down to understand how it works, then add a second query to the table 'wp_usermeta", then learn how to go to the next step and join the tables instead of running 2 queries. My first step it to see how a smaller part of the code works before moving on to more code.

include ("theme/header-main.html");

    $pdo = new PDO('mysql:host=localhost;dbname=database', 'username', 'password');
    $ps = $pdo->prepare("SELECT * FROM wp_users WHERE user_login = ?");
    if (!empty($_GET["user_login"])) {
        $ps->execute(array($_GET["user_login"]));
    } else {
        header('location: index.php');
    }

    $result = $ps->fetch(PDO::FETCH_ASSOC);

    if (is_array($result)) {
        extract($result);

        include('TESTuser-content.php');

    } else {
        include('user-content-search.php');
    }

include ("theme/footer-main.html");
  • Returning false means something went wrong. Run `$pdo->errorInfo()` to get the error – Jordan Doyle Apr 12 '14 at 13:15
  • http://stackoverflow.com/questions/15990857/reference-frequently-asked-questions-about-pdo#15990858 – Your Common Sense Apr 12 '14 at 13:20
  • Since the query appears to be right i would check: $_GET["user_login"] var and the PDO database connection. Do also a while($res = $ps->fetch()) print_r($res); to check all the result. – kawashita86 Apr 12 '14 at 13:21
  • @JordanDoyle when I added $pdo->errorInfo(); nothing additional was returned. I still get the following return value which is the same without the error Info. bool(false) –  Apr 12 '14 at 14:36
  • I might have found this issue and will comment here after I do some testing. –  Apr 12 '14 at 14:59

1 Answers1

0

I'm an idiot. :)

The code was working, but I passed the incorrect field name through the browser string. My current database uses the field of 'username" where as WordPress uses 'user_login". I changed this in the code, but was still using the following string in the URL to try and load the page. ?username=__

Sorry to have posted this, but what is done is done. Thanks to everyone that was trying to help. I really do appreciate it.