0

当我在 PHP 中运行它时,它只提取表中的第一条记录,然后显示它。我需要这个来从表中提取登录用户的数据。希望你明白我的意思:

<?php
$getall = mysql_query("SELECT name,username,email FROM users");
$row = mysql_fetch_assoc($getall);

$fullnameDB         = $row['name'];
$emailDB            = $row['email'];
$usernameDB         = $row['username'];
?>
4

3 回答 3

1

使用 mysql_fetch_assoc($getall) 您一次只获取一行。这就是为什么你应该使用“while”来获取所有内容!

$user_id = 1;
$getall = mysql_query("SELECT name,username,email FROM users WHERE user_id = $user_id");
$arr = array();
while ( $result = mysql_fetch_assoc($getall) ) {
  $arr[] = $result;
}

$date = $arr[0]["date"]; //first row date
$name = $arr[0]["name"]; //first row name
$comments = $arr[0]["comments"]; //first row comments

$arr 是一个包含每一行数据的数组。

于 2012-08-24T00:10:59.943 回答
0

更改查询:

SELECT name, username, email FROM users WHERE username = '{YourUsernameHere}' LIMIT 1
于 2012-08-23T23:58:54.513 回答
0

// 按用户id

<?php
$user_id = 1;
$getall = mysql_query("SELECT name,username,email FROM users WHERE user_id = $user_id");
$row = mysql_fetch_assoc($getall);

$fullnameDB         = $row['name'];
$emailDB            = $row['email'];
$usernameDB         = $row['username'];
?>

// 通过电子邮件和密码

<?php
$email = $_POST["email"];
$password = $_POST["password"];
$getall = mysql_query("SELECT name,username,email FROM users WHERE email = '$email' AND password = '$password'");
$row = mysql_fetch_assoc($getall);

$fullnameDB         = $row['name'];
$emailDB            = $row['email'];
$usernameDB         = $row['username'];
?>

我会通过(codeigniter 模型有查询)来做到这一点:

 $this->load->model("ModelName");
 $query = $this->ModelName->functionNameInModel(params);
 $result = $query->result();
 if ($query->num_rows() > 0)
 {
      $row = $query->row(); 
      $var = $row->table_column;
      echo("var = " . $var);
 } else {
// No records returned! HOLY ****! HUSTON WE GOT A PROBLEM!
    // Handle it with some code here...     
 }
于 2012-08-23T23:59:37.417 回答