0
<?php
$sql = "
  SELECT e.*, l.counts AS l_counts, l.date AS l_date, lo.counts AS lo_counts, lo.date AS lo_date
  FROM employee e
  LEFT JOIN logs l 
  ON l.employee_id = e.employee_id
  LEFT JOIN logout lo
  ON lo.employee_id = e.employee_id
  WHERE e.employee_id =" .(int)$_GET['salary'];

  $query = mysql_query($sql);

  $rows = mysql_fetch_array($query);

  while($countlog = $rows['l_counts']) {
  echo $countlog;
  }
echo $rows['first_name']; 
echo $rows['last_name_name']; 
?>


I got what I want to my first_name and last_name(get only 1 results). The l_counts I wanted to loop that thing but the result is not stop counting until my pc needs to restart. LoL. How can I get the exact result of that? I only need to get the exact results of l_counts.

Thank you
Jordan Pagaduan

3 Answers 3

3

you should loop over rows, not over $row[key]

while($row = mysql_fetch_array($query)) {
    echo $row['l_counts'];
}
Sign up to request clarification or add additional context in comments.

4 Comments

if I do that the $rows['last_name_name']; will be affected. I only wanted to loop is the l_counts;
then you should store the last_name_name from the first iteration for further usage. but anyway it is a bad idea. if you need in: 1) all l_counts 2) first last_name_name then you have to perform 2 queries: first one will get only l_counts and second will get only one record with one field.
is that possible? 2 select queries at a time?
not in one time: perform one query with list of all l_counts. process all the data from the response (echo them in your case). after this perform another query and process the data it returns.
1
$mysqli = new mysqli("localhost", "root", "", "test");

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

if ($result = $mysqli->query("DO HERE WHAT YOU WANT TO DO (:")) {
    while($row = $result->fetch_row()){
        echo $row[0];
    }
    $result->close();
}

Comments

1

You are using the assignment operator = not the comparison operator ==.

so essentially that line is equal to while(1) { }

what you prob meant to do as @zerkms suggested and loop over the records returned.

edit re: additional OP comments

so to get the 1st row and then loop over the rest

$firstrow = mysql_fetch_array($query);
while($rows = mysql_fetch_array($query)
{
   //fun php script
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.