使用 rowCount 从 mysql 返回字段无效
Returning a field from mysql using rowCount is not working
如果电子邮件和密码匹配,我想要以下代码 return 来自 mysql tblUser 的用户 ID。目前它没有 returning 任何东西
<?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;
}
?>
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.
相反,您可以
if($data = $stmt->fetch())
{
$result="true".$data['UserID'];
}
else
{
$result="false";
}
如果电子邮件和密码匹配,我想要以下代码 return 来自 mysql tblUser 的用户 ID。目前它没有 returning 任何东西
<?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;
}
?>
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.
相反,您可以
if($data = $stmt->fetch())
{
$result="true".$data['UserID'];
}
else
{
$result="false";
}