这是一个小问题,但我花了好几个小时试图弄清楚它为什么不起作用。
我正在尝试使用以下代码从mysql数据库中获取firstname
或lastname
:
<?php
include "config/connect.php";
$manager = 'myemail@yahoo.co.uk';
$stmt = mysqli_prepare(
$db_conx,
"SELECT lastname
FROM members
WHERE email = ?
AND lastname = ?");
//after validation, of course
mysqli_stmt_bind_param($stmt, "ss", $manager, $lastname);
mysqli_stmt_execute($stmt);
print $lastname;
if (mysqli_affected_rows($db_conx))
{
mysqli_stmt_close($stmt);//<-- CLEAN UP AFTER YOURSELF!
$id = mysqli_insert_id($db_conx);
}
?>
出于某种奇怪的原因,它根本不打印lastname
或lastname
。但如果我打印manager
,它会正确打印出来。
我做错了什么?
P.S。我知道数据库中存在email
,firstname
和lastname
。
编辑:
include "config/connect.php";
$manager = 'MYEMAIL@yahoo.co.uk';
$stmt = $db_conx->prepare("SELECT email, firstname
FROM members
WHERE email = ?
AND firstname = ?");
$stmt->bind_param('ss', $manager, $firstname);
$stmt->execute();
$stmt->bind_result($manager, $firstname);
$stmt->store_result();
if($stmt->num_rows == 1) //To check if the row exists
{
while($stmt->fetch()) //fetching the contents of the row
{
$firstname = $firstname;
$manager = $manager;
}
}
$stmt->close();
答案 0 :(得分:3)
我不明白,你想做什么,
如果你想获得FIRSTNAME和LASTNAME
$stmt = mysqli_prepare($db_conx,"SELECT firstname,lastname FROM members WHERE email = ?");
$stmt->bind_param('s', $manager);
$stmt->execute();
$stmt->bind_result($fname, $lname);
/* fetch values */
while ($stmt->fetch()) {
printf("%s %s\n", $fname, $lname);
}