我想使用以下代码获取数据:
if(isset($_POST['btnSubmit']))
{
/* connect using pdo */
try
{
$db_conn = new PDO('mysql:host=localhost; dbname =database','username','password');
}
catch (PDOException $e) {
echo "could not connect to database.";
exit;
}
$password=$_POST['password'];
$selectSQL = "SELECT fac_id, username, password, type, user_prefix FROM faculty WHERE username = ? AND password = ?";
$stmt = $db_conn->prepare($selectSQL);
$stmt->execute(array($_POST['username'],$password));
while($abc = $stmt->fetch())
{
print_r($abc);
}
echo $rowCount = count($stmt);
die;
}
所有行+行数。
仅限行数。
答案 0 :(得分:2)
嗯,您没有检查错误(如果您不知道,您的查询很可能会失败)。
要检查错误,请将PDO::ATTR_ERRMODE
设置为PDO::ERRMODE_EXCEPTION
。 (这将导致PDO在错误上抛出PDOExceptions):
if(isset($_POST['btnSubmit']))
{
/* connect using pdo */
try
{
$db_conn = new PDO('mysql:host=localhost; dbname =database','username','password');
$db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db_conn->setAttribute(PDO::EMULATE_PREPARES, false); //This will prevent PDO from emulating prepared statements. Solving various edge cases.
$password=$_POST['password'];
$selectSQL = "SELECT fac_id, username, password, type, user_prefix FROM faculty WHERE username = ? AND password = ?";
$stmt = $db_conn->prepare($selectSQL);
$stmt->execute(array($_POST['username'],$password));
while($abc = $stmt->fetch())
{
print_r($abc);
}
echo $rowCount = count($stmt);
die;
}
catch (PDOException $e) {
echo "There was an error! " . $e->getMessage();
exit;
}
}
答案 1 :(得分:0)
具有讽刺意味的是,你正在进行你的抓取,但它会计算你做错了什么 因此,如果没有提取 - 没有数据可供匹配。
if(isset($_POST['btnSubmit']))
{
$dsn = "mysql:host=localhost;dbname=database;charset=utf8";
$opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$pdo = new PDO($dsn,'username','password', $opt);
$sql = "SELECT fac_id, username, password, type, user_prefix
FROM faculty WHERE username = ? AND password = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute(array($_POST['username'],$_POST['password']));
$user = $stmt->fetch();
var_dump($user);
}
如果说FALSE,你需要调试一些 尝试检查密码,例如
$sql = "SELECT password FROM faculty WHERE username = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute(array($_POST['username']));
$pass = $stmt->fetchColumn();
var_dump($pass, $_POST['password'], $_POST['password'] == $pass);
答案 2 :(得分:0)
show all column name in bind result
$result = $conn->prepare('SELECT * FROM users WHERE user_email = ? and user_password = ? ');
$result->bind_param('ss', $email, $password);
$result->execute();
$result->bind_result($userid,$fullname,$useremail,$userpassword,$userimage);
$array = array();
while($result->fetch())
{
$array['id'] = $userid;
$array['fullname'] = $fullname;
$array['email'] = $useremail;
$array['picture'] = $userimage;
}
print_r($array);