Emily
Emily

Reputation: 1151

Error using PDO to Echo a single row from database

Hi_I'm trying to get the value of a single row from my database using a PDO SELECT statement, then echo it back from the PHP file to my Javascript file.

If the value equals 1 then I want to create an alert to say "The value is 1". If the value doesn't equal 1 then I want to create an alert to say "The value is not 1".

I've read other posts about similar problems but the questions seem to involve arrays, but I only want a single value. I think I almost have the correct Javascript and PHP/PDO code to do this, but I think there's something wrong with the PDO.

The datatype of the row I'm selecting is VARCHAR. I'm sending a user ID (retrieved from Facebook's Graph API) by an AJAX POST to the PHP file, I then use bindParam to prepare it to be included in the query.

console.log is returning this error message: Warning: log() expects parameter 1 to be double, array given in ../scripts/valuecheck.php on line 29

This is my Javascript & AJAX POST:

function checkValue() {

FB.login(function(response) {
  if (response.authResponse) {
    console.log('Welcome!  Fetching your information.... ');

     FB.api('/me', function(response) {

     if (response.error) {
       console.log('Error - ' + response.error.message);
     }
     else {
       var userid = response.id;

                            $.ajax({
         url: 'scripts/valuecheck.php',
         data: {'userid' : userid},
        type: "POST",
        success: function(response){
            console.log(response);
                 if(response==1){
                         alert( "The value is 1");
                         }
                    else{
                        alert( "The value is not 1");
                              }
                    }
       });
}
    });
  } else {
    console.log('User cancelled login or did not fully authorize.');
  }
 });
}

This is my PHP file:

<?php

$servername = "myservername";
$username = "myusername";
$password = "mypassword";
$dbname = "mydbname";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


$stmt = $conn->prepare("SELECT `value` FROM `table1` WHERE `id` = :currentuserid"); 
$stmt->bindParam(':currentuserid', $currentuserid);
$currentuserid = $_POST['userid'];
$stmt->execute(); 

$result = $stmt->fetch(); 
echo $result;
    }

catch(PDOException $e)
{
    echo "Error: " . $e->getMessage();
}
$conn = null;
console.log($result);
?>

Any help on how to fix this would be greatly appreciated! Thanks in advance.

Upvotes: 1

Views: 390

Answers (2)

Marc B
Marc B

Reputation: 360692

You're mixing JS and PHP:

catch(PDOException $e)
{
    echo "Error: " . $e->getMessage();
}
$conn = null;
console.log($result);
^^^^^^^^^^^^^^^^^^^---this is javascript, not PHP.

Upvotes: 1

Barmar
Barmar

Reputation: 781028

fetch returns an array, with an element for each column in the SELECT, so you need to use:

echo $result['value'];

Upvotes: 2

Related Questions