Reputation: 29
When I run this in PHP, it only pulls the first record in the table and then displays it. I need this to pull the data for the user that is logged in out of the table. Hopefully you know what I mean:
<?php
$getall = mysql_query("SELECT name,username,email FROM users");
$row = mysql_fetch_assoc($getall);
$fullnameDB = $row['name'];
$emailDB = $row['email'];
$usernameDB = $row['username'];
?>
Upvotes: 0
Views: 180
Reputation: 858
With mysql_fetch_assoc($getall) you are fetching just one row at a time. That's why you should use "while" to get all!
$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 is an array holding each row's data.
Upvotes: 1
Reputation: 1987
// By user 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'];
?>
// By email and password
<?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'];
?>
I would do it via (codeigniter model has the query):
$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...
}
Upvotes: 0
Reputation: 1358
Change the query:
SELECT name, username, email FROM users WHERE username = '{YourUsernameHere}' LIMIT 1
Upvotes: 0