-2

I'm trying to get this PDO fetch to display the number of points a user has. However, I simply just get the output "array". What have I done wrong here, and is there an easier way to fetch a single row from a column than this?

// CONNECT TO DATABASE
require 'database.php';

// FETCH POINTS
$usernamedb = $_SESSION['user'];
echo $usernamedb;

$fetchpoints = $db->prepare("SELECT points FROM login WHERE username = '$usernamedb'");
$fetchpoints->execute();

$pointsfetched = $fetchpoints->fetchAll();
echo $pointsfetched;
Gjert
  • 1,069
  • 1
  • 18
  • 48
  • pdo prepare - http://stackoverflow.com/questions/1457131/php-pdo-prepared-statements – fortune Dec 16 '14 at 17:23
  • 1
    Since you're already using PDO, you could also adopt prepared statements now. Use `->prepare("SELECT .. WHERE x = ?")` and `->execute(array($usernamedb));` for binding the `?` placeholder. (Just because your string originates in a session, doesn't ensure it's implicitly constrained. Better safe than sorry!) – mario Dec 16 '14 at 17:23
  • 2
    How have you gotten to the point where you have a working website in PHP using a database connection while not knowing (and apparently not being able to find out) how to read a value from an array? – GolezTrol Dec 16 '14 at 17:26
  • I used to just do it like I typed it now, but for some reason it didn't work this time... I'm not sure what I have done differently.. – Gjert Dec 16 '14 at 17:28
  • Here is the code I got: http://www.codeshare.io/EBwU6 – Gjert Dec 16 '14 at 19:00

4 Answers4

1

All calls to PDO::fetch or PDO::fetchAll will return an array. If you only want to get one column from the first row, you need to use PDO::fetchColumn

//Use 0 to get the first column
$pointsfetched = $fetchpoints->fetchColumn(0);

PDO::fetch will always return an array of the next row. (or false)

PDO::fetchAll will always return an array of row arrays. (or false)

Eric G
  • 3,427
  • 5
  • 28
  • 52
1

If you just want a single value out of the query, then don't use fetchAll() to get an array/list.

$pointsfetched = $fetchpoints->fetchObject()->points;

Is often the most concise option.
(fetch and fetchObject just pull a single row from the result set.)

mario
  • 144,265
  • 20
  • 237
  • 291
  • This worked for me, thank you :) I dont know why I couldn't use the same method as I used before... has there been any updates recently? – Gjert Dec 16 '14 at 17:31
  • I have another question @mario, why doesn't this work?`$usernamedb = $_SESSION['user']; $fetchone = $db->prepare("SELECT profilepicture,name,points FROM login WHERE username = '$usernamedb'"); $personalprofile = $fetchone->fetchAll(); echo " – Gjert Dec 16 '14 at 18:40
  • That code and the array access actually look ok. What does `print_r()` tell about the result list, and how does the resulting HTML output look (`view source`, there's no closing `'>` img tag end)? – mario Dec 16 '14 at 18:43
  • print_r() gives me nothing, I have the closing tag, just didn't add it with :) I just get in the view source – Gjert Dec 16 '14 at 18:53
  • Then probe the `$usernamedb` instead. Possibly not set (session_start missing etc.), resulting in a failing query and absent values. – mario Dec 16 '14 at 18:56
0

Start by changing:

echo $pointsfetched;

to:

echo json_encode($pointsfetched);

This will show you the data you have fetched in JSON format, if it is working. You can then use something like:

foreach ($pointsfetched as $point) {
    echo $point;
}

to display all the points.

rjmunro
  • 27,203
  • 20
  • 110
  • 132
  • This just made me get `d[{"points":"12524","0":"12524"}]`. I am simply looking to get 12524 from this. – Gjert Dec 16 '14 at 17:23
0

Change $fetchpoints->fetchAll() to $fetchpoints->fetchColumn()

This way you'll only get the first point as a typed var instead of an array of one point

GPierre
  • 100
  • 11