1

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

表 (bird_playlog) 如下所示:

interpret: Tiny Dancers 

title: Bonfire Of The Night                 

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

截屏:

在此处输入图像描述

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

但是脚本没有显示 $last_track[0]['interpret'];,那么有什么问题吗?我没有错误信息...

谢谢你的帮助!大卫

更新:

这有效:

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

1 回答 1

1

我认为如果你不想,你应该通过 null 。因为该函数最初有 7 个要接受的参数,而您只传递了 5 个,所以它不会按您的意愿使用。似乎你不想要的 $key 比 for $key 你应该传递 null 。因为如果你传递 5 个参数,那么函数会将它作为前 5 个参数,即使你不想要,最后两个也会被作为默认参数。我希望这能解决你的问题。

于 2012-09-21T11:23:52.357 回答