MySQL last entry PHP sorted by date

I want to display the latest entry from a MySQL database with PHP.

The table (bird_playlog) looks like that:

interpret: Tiny Dancers 

title: Bonfire Of The Night                 

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

Screenshot:

The MySQL Connect script:

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

} ?>

And the script which shows the latest entry looks like this:

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

But the script doesn't show $last_track[0]['interpret'];, so what is wrong? I have no error message...

Thanks for your help! David

UPDATE:

This works:

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

Answers


I think you should pass null if you don't want. Because the function originally have 7 parameters to accept and you are passing only 5 so it will not take as you want. It seems that $key you dont want than at for $key you should pass null. Because if you pass 5 parameter than function will take it as the first 5 parameters and the last two will be taken as default even if you don't want. I hope this will solve your problem.


Need Your Help

get the characters available in TrueType with PHP

php fonts gd true-type-fonts

How do I get a list of all characters available in the TrueType font? using PHP.

different app name for iphone-ipad

xcode app-store

We have uploaded an app to the appstore with the name "appName" . its for Ipad only.