Only Pulling First Record In Database
Frecuentes
Visto 172 veces
0
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'];
?>
3 Respuestas
1
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.
Respondido 24 ago 12, 01:08
0
Cambia la consulta:
SELECT name, username, email FROM users WHERE username = '{YourUsernameHere}' LIMIT 1
Respondido 24 ago 12, 00:08
0
// 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...
}
Respondido 24 ago 12, 01:08
Darcey, is there another I can talk to you other then here? Maybe Skype? - ImagineCustoms
Im on google talk - Darcey.Lloyd@gmail.com and PM/IM me... My mic is in some box somewhere. - Darcey
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas php or haz tu propia pregunta.
You've only told it to pull the first record.... what is the problem? Missing a
WHERE
¿cláusula? - Brad