我猜这些问题都说明了一切。我的查询结果会产生一个与该条件匹配的行。我想从每个表列中获取每个数据并放入变量。
$getinfo = "select
user_firstname,
user_middlename,
user_lastname
from tempuserinfo
where user_email ='$ead'
and activation_code='$eac'";
$query = mysql_query($getinfo, $db);
if(!$query) {
echo'something went wrong.';
} else {
//put them into variables
$firstname = mysql_fetch_object($query, 'user_firstname');
$middlename = mysql_fetch_object($query, 'user_middlename');
$lastname = mysql_fetch_object($query, 'user_lastname');
}
答案 0 :(得分:5)
如果您获得多个结果,可以像这样循环遍历:
$getinfo = "select user_firstname, user_middlename, user_lastname from tempuserinfo where user_email ='$ead' and activation_code='$eac'";
$query = mysql_query($getinfo, $db);
while ($row = mysql_fetch_array($query)) {
$firstname = $row['user_firstname'];
$lastname = $row['user_lastname'];
}
如果您只返回一行(确保在SQL语句中添加LIMIT),那么您可以使用以下内容:
$getinfo = "select user_firstname, user_middlename, user_lastname from tempuserinfo where user_email ='$ead' and activation_code='$eac'";
$query = mysql_query($getinfo, $db);
$row = mysql_fetch_array($query);
$firstname = $row['user_firstname'];
$lastname = $row['user_lastname'];
答案 1 :(得分:1)
mysql_ *函数已弃用。在文档中,建议像最新版本的PHP一样,他们将使用mysqli_ *或PDO。
以下是使用mysqli解决问题的脚本:
$mysqli = new mysqli("localhost", "root_user", "root_password", "database");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$getinfo = "select
user_firstname,
user_middlename,
user_lastname
from tempuserinfo
where user_email ='$ead'
and activation_code='$eac'";
if ($result = $mysqli->query($getinfo)) {
while ($row = $result->fetch_object()) {
$firstname = $row->user_firstname;
$middlename = $row->user_middlename;
$lastname =$row->user_lastname;
}
$result->close();
}
else
{
echo'something went wrong.';
}
答案 2 :(得分:1)
mysqli的例子,mysql不推荐使用
$sql = "select user_firstname, user_middlename, user_lastname from tempuserinfo where user_email ='$ead' and activation_code='$eac'";
$query = mysqli_query($conn, $sql);
while($rs = mysqli_fetch_assoc($query)){
$user_firstname = $rs['user_firstname'];
$user_middlename = $rs['user_middlename'];
$user_lastname = $rs['user_lastname'];
}
如果您想要拥有所有字段以便稍后查阅它们,请将它们添加到数组
$i=0;
while($rs = mysqli_fetch_assoc($query)){
$data[$i] = array('user_firstname'=>$rs['user_firstname'], 'user_middlename'=>$rs['user_middlename'], 'user_lastname'=> $rs['user_lastname']);
$i++;
}
print_r($data);
答案 3 :(得分:0)
这应该有效:
$result = mysql_fetch_object($query);
$firstname = $result->user_firstname;
$middlename = $result->user_middlename;
$lastname = $result->user_lastname;
<强> BUT:强> 使用MySQLi或PDO sintead mysql模块已经弃用
答案 4 :(得分:0)
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
try {
$sth = $dbh->prepare("select user_firstname, user_middlename, user_lastname from tempuserinfo where user_email = ? and activation_code=?");
$sth->execute(array($ead,$eac));
$row = $sth->fetch(PDO::FETCH_BOTH);
$user_firstname = $row['user_firstname'];
$user_middlename = $row['user_middlename'];
$user_lastname = $row['user_lastname'];
} catch (PDOException $e) {
echo 'Database operation failed: ' . $e->getMessage();
}
真的,确实要学会使用PDO,因为它就像使用mysql_ *一样简单,但更安全,更好。