LESETJA LESETJA - 3 years ago 159
MySQL Question

Returning a field from mysql is not working

I want the following code to return the userID from mysql tblUser of the user if the email and password matched. Currently it is not returning anything

<?php

include 'config.inc.php';

// Check whether username or password is set from android
if(isset($_POST['email']) && isset($_POST['password']))
{
// Innitialize Variable
$result='';
$email = $_POST['email'];
$password = $_POST['password'];

// Query database for row exist or not
$sql = 'SELECT UserID FROM tblUser WHERE email = :email AND password = :password';
$stmt = $conn->prepare($sql);
$stmt->bindParam(':email', $email, PDO::PARAM_STR);
$stmt->bindParam(':password', $password, PDO::PARAM_STR);
$stmt->execute();
if($stmt->rowCount())
{
$result="true" . UserID;
}
elseif(!$stmt->rowCount())
{
$result="false";
}

// send result back to android
echo $result;
}

?>

Answer Source

For most databases, PDOStatement::rowCount() does not return the number of rows affected by a SELECT statement. Instead, use PDO::query() to issue a SELECT COUNT(*) statement with the same predicates as your intended SELECT statement, then use PDOStatement::fetchColumn() to retrieve the number of rows that will be returned. Your application can then perform the correct action.

instead, you could do

      if($data = $stmt->fetch())
      {
         $result="true".$data['UserID'];
      }  
      else
      {
            $result="false";
      }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download