2012-06-11 2 views
1

PHP로 MySQL 데이터베이스의 최신 항목을 표시하고 싶습니다.MySQL 마지막 항목 날짜순 정렬 PHP

표 (bird_playlog)는 그 다음과 같습니다

interpret: Tiny Dancers 

title: Bonfire Of The Night     

date: 2012-06-11 14:30:58 

스크린 샷 :

enter image description here

MySQL의 연결 스크립트 :

<?php 
class mw_sql{ 

    private $host; 
    private $user; 
    private $pass; 
    private $db; 
    private $connection = null; 
    public $connected = false; 

    public function __construct($data){ 
     $this->host = $data['host']; 
     $this->user = $data['user']; 
     $this->pass = $data['pass']; 
     $this->db = $data['db']; 
    } 

    public function __destruct(){ 
     if($this->connected) mysql_close($this->connection); 
    } 

    public function connect(){ 
     $this->connection = mysql_connect($this->host, $this->user, $this->pass, true); 
     if(!$this->connection){ 
      echo '<pre>MySQL connect failed</pre>'; 
      $this->connected = false; 
     }else{ 
      if(@mysql_select_db($this->db, $this->connection)){ 
       $this->connected = true; 
      }else{ 
       echo '<pre>MySQL select db failed</pre>'; 
       echo '<pre>'.mysql_error($this->connection).'</pre>'; 
       $this->connected = false; 
      } 
     } 
     return $this->connected; 
    } 

    public function select($table, $fields=null, $key=null, $where=null, $sort=null, $sort_dir='ASC', $limit=null){ 
     $cols = (is_array($fields) && $fields != null) ? mysql_real_escape_string(implode(', ', $fields), $this->connection) : '*'; 
     $where_clause = ($where != null) ? ' WHERE '.$where : ''; 
     $sort_clause = ($sort != null) ? ' ORDER BY '.$sort.' '.$sort_dir : ''; 
     $limit_clause = ($limit != null) ? ' LIMIT '.$limit : ''; 
     $query = "SELECT ".$cols." FROM ".$table.$where_clause.$sort_clause.$limit_clause; 
     $res = @mysql_query($query, $this->connection); 
     if(!$res){ 
      return false; 
     }else{ 
      $data = array(); 
      if(mysql_num_rows($res) > 0){ 
       while($dat = mysql_fetch_assoc($res)){ 
        if($key == null) array_push($data, $dat); 
        else $data[$dat[$key]] = $dat; 
       } 
      } 
      return $data; 
     } 
    } 

    public function query($query){ 
     $res = @mysql_query($query, $this->connection); 
     if(!$res){ 
      echo mysql_error(); 
      return false; 
     }else{ 
      return $res; 
     } 
    } 

    public function insert($table, $fields, $values){ 
     $vals = array(); 
     foreach($values as $value){ 
      array_push($vals, mysql_real_escape_string($value, $this->connection)); 
     } 
     $query = "INSERT INTO ".$table." (".mysql_real_escape_string(implode(', ', $fields), $this->connection).") VALUES ('".implode("', '", $vals)."')"; 
     $res = @mysql_query($query, $this->connection); 
     if(!$res){ 
      pre(mysql_error($this->connection)); 
      return false; 
     } 
     return true; 
    } 

    public function update($table, $fields, $values, $where, $error_no_rows=true){ 
     $update = array(); 
     foreach($fields as $key => $value){ 
      if($values[$key] == 'increment'){ 
       array_push($update, $value."=".$value.'+1'); 
      }else{ 
       array_push($update, mysql_real_escape_string($value, $this->connection)."='".mysql_real_escape_string($values[$key], $this->connection)."'"); 
      } 
     } 
     $query = "UPDATE ".$table." SET ".implode(', ', $update)." WHERE ".$where; 
     $res = @mysql_query($query, $this->connection); 
     if(!$res){ 
      pre(mysql_error($this->connection)); 
      return false; 
     } 
     if(mysql_affected_rows($this->connection) == 0 && $error_no_rows){ 
      return false; 
     } 
     return true; 
    } 

    public function delete($table, $where){ 
     $query = "DELETE FROM ".$table." WHERE ".$where; 
     echo '<pre>'.$query.'</pre>'; 
     $res = @mysql_query($query, $this->connection); 
     if(!$res) return false; 
     return true; 
    } 

} ?> 

그리고 최신 항목을 보여줍니다 스크립트는 다음과 같습니다

<?php 

require_once('mw_sql.class.php'); 

$cuelist_db_conf = array(
    'host' => 'w00b2ffc.kasserver.com', 
    'user' => 'd0144421', 
    'pass' => '****', 
    'db' => 'd0144421', 
    'table' => 'bird_playlog' 
); 

$cuelist_db = new mw_sql($cuelist_db_conf); 
$cuelist_db->connect(); 

$last_track = $cuelist_db->select('bird_playlog', array('interpret', 'title'), 'date', 'DESC', 1); 

echo $last_track[0]['interpret']; ?> 

는 그러나 스크립트 [0] ;, 그래서 뭐가 잘못이다 '해석'] $의 last_track을 표시하지 않습니다? 오류 메시지가 없습니다 ...

당신의 도움에 감사드립니다! 데이비드

UPDATE :

이 작동 :

$con = mysql_connect("w00b2ffc.kasserver.com","d0144421","****"); 
if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 

mysql_select_db("d0144421", $con); 

$last_track = mysql_query("SELECT * FROM bird_playlog ORDER BY date DESC LIMIT 1"); 

while($row = mysql_fetch_assoc($last_track)) { 
extract($row); 
} 
+3

mysql 호출에서'@'기호를 제거하여 오류를 방지하십시오. – prodigitalson

+1

모든 mysql 명령 앞에'@'기호를 사용하지 않으므로 오류 메시지가 나타나지 않을 것입니다. 예 : '$ res = @mysql_query ($ query, $ this-> connection);'그들을 제거하고 오류가 있는지보십시오. –

+0

그래, 나는 @ 기호를 제거했지만 아무런 오류가 발생하지 않는다 ... – David

답변

1

내가 원하지 않는 경우 null을 전달한다고 생각합니다. 함수는 원래 7 개의 매개 변수를 받아들이고 5 개만 전달하므로 원하는대로 가져 가지 않습니다. $ key는 null을 전달해야합니다. 함수보다 5 매개 변수를 전달하면 처음 5 개의 매개 변수로 가져오고 나머지 두 매개 변수는 원하지 않는 경우에도 기본 매개 변수로 사용됩니다. 문제가 해결되기를 바랍니다.