Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL last entry PHP sorted by date
    primarykey
    data
    text
    <p>I want to display the latest entry from a MySQL database with PHP. </p> <p><strong>The table (bird_playlog) looks like that:</strong> </p> <pre><code>interpret: Tiny Dancers title: Bonfire Of The Night date: 2012-06-11 14:30:58 </code></pre> <p>Screenshot: </p> <p><img src="https://i.stack.imgur.com/h0ZP2.png" alt="enter image description here"></p> <p><strong>The MySQL Connect script:</strong></p> <pre><code>&lt;?php class mw_sql{ private $host; private $user; private $pass; private $db; private $connection = null; public $connected = false; public function __construct($data){ $this-&gt;host = $data['host']; $this-&gt;user = $data['user']; $this-&gt;pass = $data['pass']; $this-&gt;db = $data['db']; } public function __destruct(){ if($this-&gt;connected) mysql_close($this-&gt;connection); } public function connect(){ $this-&gt;connection = mysql_connect($this-&gt;host, $this-&gt;user, $this-&gt;pass, true); if(!$this-&gt;connection){ echo '&lt;pre&gt;MySQL connect failed&lt;/pre&gt;'; $this-&gt;connected = false; }else{ if(@mysql_select_db($this-&gt;db, $this-&gt;connection)){ $this-&gt;connected = true; }else{ echo '&lt;pre&gt;MySQL select db failed&lt;/pre&gt;'; echo '&lt;pre&gt;'.mysql_error($this-&gt;connection).'&lt;/pre&gt;'; $this-&gt;connected = false; } } return $this-&gt;connected; } public function select($table, $fields=null, $key=null, $where=null, $sort=null, $sort_dir='ASC', $limit=null){ $cols = (is_array($fields) &amp;&amp; $fields != null) ? mysql_real_escape_string(implode(', ', $fields), $this-&gt;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-&gt;connection); if(!$res){ return false; }else{ $data = array(); if(mysql_num_rows($res) &gt; 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-&gt;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-&gt;connection)); } $query = "INSERT INTO ".$table." (".mysql_real_escape_string(implode(', ', $fields), $this-&gt;connection).") VALUES ('".implode("', '", $vals)."')"; $res = @mysql_query($query, $this-&gt;connection); if(!$res){ pre(mysql_error($this-&gt;connection)); return false; } return true; } public function update($table, $fields, $values, $where, $error_no_rows=true){ $update = array(); foreach($fields as $key =&gt; $value){ if($values[$key] == 'increment'){ array_push($update, $value."=".$value.'+1'); }else{ array_push($update, mysql_real_escape_string($value, $this-&gt;connection)."='".mysql_real_escape_string($values[$key], $this-&gt;connection)."'"); } } $query = "UPDATE ".$table." SET ".implode(', ', $update)." WHERE ".$where; $res = @mysql_query($query, $this-&gt;connection); if(!$res){ pre(mysql_error($this-&gt;connection)); return false; } if(mysql_affected_rows($this-&gt;connection) == 0 &amp;&amp; $error_no_rows){ return false; } return true; } public function delete($table, $where){ $query = "DELETE FROM ".$table." WHERE ".$where; echo '&lt;pre&gt;'.$query.'&lt;/pre&gt;'; $res = @mysql_query($query, $this-&gt;connection); if(!$res) return false; return true; } } ?&gt; </code></pre> <p><strong>And the script which shows the latest entry looks like this:</strong> </p> <pre><code>&lt;?php require_once('mw_sql.class.php'); $cuelist_db_conf = array( 'host' =&gt; 'w00b2ffc.kasserver.com', 'user' =&gt; 'd0144421', 'pass' =&gt; '****', 'db' =&gt; 'd0144421', 'table' =&gt; 'bird_playlog' ); $cuelist_db = new mw_sql($cuelist_db_conf); $cuelist_db-&gt;connect(); $last_track = $cuelist_db-&gt;select('bird_playlog', array('interpret', 'title'), 'date', 'DESC', 1); echo $last_track[0]['interpret']; ?&gt; </code></pre> <p>But the script doesn't show $last_track[0]['interpret'];, so what is wrong? I have no error message...</p> <p>Thanks for your help! David</p> <p><strong>UPDATE:</strong></p> <p>This works: </p> <pre><code>$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); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload