简体   繁体   中英

PHP mysql select results

<?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

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

while($row = mysql_fetch_array($query)) {
    echo $row['l_counts'];
}

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
}
$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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM