-4

I've had a look round on google and a couple of other sites, but I can't figure out why this isn't working.

I'm working on a uni assignment, and run the following code:

$db = connect();
$testname = 'chunk';
 try {
        $stmt = $db->prepare('SELECT * FROM TreasureHunt.Player WHERE name = ?');
        $stmt->execute(array($testname));
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
        $stmt->closeCursor();
        } 
    catch (PDOException $e) { 
    print "Error checking login: " . $e->getMessage(); 
}

$count = $stmt->rowCount();
echo 'Row count: ', $count, '<br>';
var_dump($result);
echo '<br>Result: ', $result['name'], ' pw: ', $result["password"], '<br>';
print_r($result);

However this results in the following errorm:

Notice: Undefined index: name in /.automount/net/ufiler2/u2/cs3/dbes2376/lib/html/th/index.php on line 30

Now I know that means, that in my $result variable the index 'name' must not exist. But this confuses me because it does exist, checked by looking in: var_dump($result) var_dump($result) gives me:

array(1) { [0]=> array(5) { ["name"]=> string(5) "chunk" ["password"]=> string(7) "truffle" ["pw_salt"]=> string(8) "g#m#g#m#" ["gender"]=> string(1) "m" ["addr"]=> string(10) "Goon Docks" } } 

and print_r($result) gives me:

Array ( [0] => Array ( [name] => chunk [password] => truffle [pw_salt] => g#m#g#m# [gender] => m [addr] => Goon Docks ) )

So why can I not refer to them as $result['name'] ? Or how do I actually refer to it?

Thanks. Sorry if this is a basic question.

Max
  • 107
  • 7
  • As you can see from the output of `var_dump($result)` , it's an array with another array in it. – Ofir Baruch May 27 '13 at 13:08
  • Wow, that's a lot of unexplained downvotes, especially for a question that's actually fairly well written. Downvoters, kindly comment too? – Craig Ringer May 27 '13 at 13:36

4 Answers4

3

You should be using $result[0]['name'];

Sirko
  • 72,589
  • 19
  • 149
  • 183
2

fetchAll() returns an array of arrays. So in your case access

$result[0]['name']

instead of

$result['name']

Another option would be to use just fetch() in order to get only the one row of the result each time.

Sirko
  • 72,589
  • 19
  • 149
  • 183
0

You have several errors in your code. I have rewritten it to hopefully remove them, and get it working as expected:

$db = connect();
$testname = 'chunk';

try {
    $stmt = $db->prepare('SELECT * FROM TreasureHunt.Player WHERE name = ?');
    $stmt->execute(array($testname));

    foreach($stmt->fetchAll(PDO::FETCH_ASSOC) as $row){
        echo 'Result: ' . $result['name'] . ' pw: ' . $result['password'] . '<br />';
    }
} catch (PDOException $e) { 
    print "Error checking login: " . $e->getMessage(); 
}

$count = $stmt->rowCount();
echo 'Row count: ' . $count .  '<br>';
Phil Cross
  • 9,017
  • 12
  • 50
  • 84
0

You have several errors in your code. I have rewritten it to hopefully remove them, and get it working as expected. You have to also connect as described in the tag wiki

$db = connect();
$testname = 'chunk';
$stmt = $db->prepare('SELECT * FROM TreasureHunt.Player WHERE name = ?');
$stmt->execute(array($testname));
$row = $stmt->fetch();

$count = $stmt->rowCount();
echo 'Row count: ', $count, '<br>';
echo '<br>Result: ', $row['name'], ' pw: ', $row["password"], '<br>';
Community
  • 1
  • 1
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345