MySQL最后一个条目PHP按日期排序

时间:2012-06-11 13:31:55

标签: php mysql database

我想用PHP显示MySQL数据库中的最新条目。

表格(bird_playlog)看起来像这样:

interpret: Tiny Dancers 

title: Bonfire Of The Night                 

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

屏幕截图:

enter image description here

MySQL Connect脚本:

<?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']; ?>

但脚本没有显示$ last_track [0] ['interpre'] ;,那么有什么不对?我没有错误信息......

感谢您的帮助!大卫

更新

这有效:

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

1 个答案:

答案 0 :(得分:1)

我认为如果你不想要的话你应该传递null。 因为该函数最初有7个参数可以接受而你只传递5个所以它不会按你的意愿去。看起来你不想要的$ key,而不是$ key,你应该传递null。 因为如果传递5个参数而不是函数将把它作为前5个参数,最后两个将被视为默认值,即使你不想要。 我希望这能解决你的问题。