如何将SELECTED元素从数据库放入变量中

时间:2014-01-15 11:27:46

标签: php mysql

如何将这些元素放入变量?

$user_information = $connect->prepare("SELECT email, gender, firstname, middlename, FROM members");

喜欢

$firstname = 'firstname';
$gender = 'gender';
$email = 'email';

我该怎么做

提前致谢

4 个答案:

答案 0 :(得分:1)

如果您使用的是PDO

  $user_information ->execute();
  $result = $user_information->fetch(PDO::FETCH_ASSOC);
  $firstname = $result['firstname'];
  $gender = $result['gender'];
  $email = $result['email'];

如果您使用的是mysqli

$user_information->execute();
$user_information->bind_result($firstname, $gender, $email);
$user_information->fetch();
printf("%s, %s, %s\n", $firstname, $gender, $email);
$user_information ->close();

答案 1 :(得分:0)

如果您使用的是PHP和PDO,则可以使用fetch方法使用PDO::FETCH_OBJ获取对象。 http://de3.php.net/manual/en/pdostatement.fetch.php

$result = $sth->fetch(PDO::FETCH_OBJ);
print $result->NAME;

答案 2 :(得分:0)

list($email, $gender, $firstname, $middlename) = $user_information;

有关详细信息,请查看LIST function @ PHP docs

答案 3 :(得分:0)

如果它返回一个对象你可以得到它;

$firstname = $user_information->firstname;
$gender = $user_information->gender;
$email = $user_information->email;

或者如果它返回一个数组;

$firstname = $user_information["firstname"];
$gender = $user_information["gender"];
$email = $user_information["email"];