user3233623
user3233623

Reputation: 383

Get PDO to work

After working with PDO for a while yesterday, I tested a variation of this code. Luckily it worked last night and was able to echo the result for the first time in google chrome. I started from scratch today and now it doesn't print anything. Any ideas?

<?php

$an_int = 12;    

// If this is an integer

if (is_int($an_int)) 

{

$conn = new PDO('mysql:host=localhost;dbname=pushchat', 'pushchat', 'd]682\#%yI1nb3');
    global $conn;

$stmt = $conn->prepare("SELECT IdPhoto, device_token, IdUser FROM photos ORDER BY IdPhoto DESC LIMIT 300 ");

$stmt->execute();

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

echo "$result";
}
?>

Can someone help properly format the code above so that it can access my database using pdo? How should one set up a test document with PDO capabilities like this? When I try to print $result the browser just says : Array.

Upvotes: 1

Views: 37

Answers (1)

hmjha
hmjha

Reputation: 489

PDO::FETCH_ASSOC: returns an array indexed by column name as returned in your result set. So use print_r() instead of echo. Even for echo any variable you don't need to put ""

<?php    
    $an_int = 12;    

    // If this is an integer

    if (is_int($an_int)) 

    {

    $conn = new PDO('mysql:host=localhost;dbname=pushchat', 'pushchat', 'd]682\#%yI1nb3');
        global $conn;

    $stmt = $conn->prepare("SELECT IdPhoto, device_token, IdUser FROM photos ORDER BY IdPhoto DESC LIMIT 300 ");

    $stmt->execute();

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

    print_r($result);
    }
    ?>

Upvotes: 2

Related Questions