tags:

views:

38

answers:

3
<?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  A: 

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

while($row = mysql_fetch_array($query)) {
    echo $row['l_counts'];
}
zerkms
if I do that the $rows['last_name_name']; will be affected. I only wanted to loop is the l_counts;
Jordan Pagaduan
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_count`s and second will get **only** one record with one field.
zerkms
is that possible? 2 select queries at a time?
Jordan Pagaduan
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.
zerkms
+1  A: 

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
}
Geek Num 88
+1  A: 
$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();
}
GaVrA