Current location: Hot Scripts Forums » Other Discussions » Database » Video SELECT LAST database


Video SELECT LAST database

Reply
  #1 (permalink)  
Old 12-19-09, 11:12 AM
timbaker1991's Avatar
timbaker1991 timbaker1991 is offline
Newbie Coder
 
Join Date: Dec 2009
Location: London, UK
Posts: 38
Thanks: 2
Thanked 3 Times in 3 Posts
Video SELECT LAST database

Oky, trying to display videos on a website by drawing informtaion from a database table, but only want to display the most recent video, i.e. the last record, I have tried a few solutions but can't get it to work, heres my code if anyone could help me would really appericiate it!


<?php
$con = mysql_connect(" ************ "," ************ "," ********* ");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("clairescou1", $con);

$result = mysql_query(" SELECT (video_url), FROM video");
while($row = mysql_fetch_array($result))
{
echo $row['video_ID'] . " " . $row['video_url'];
echo "<br />";
}

mysql_close($con);
?>

(for obvious reasons i've removed the passwords/address etc!)
Reply With Quote
  #2 (permalink)  
Old 12-19-09, 02:22 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
To do any kind of sorting (first, last, etc) you need to have some sort of key with which to sort by, like an ID or an upload date. You have "video_ID", which hopefully is an auto-incrementing number. If that's the case, then you can use SQL something like this:

SELECT * FROM video ORDER BY video_ID DESC LIMIT 1

This selects 1 row, ordered by the video_ID in descending order. The result is that you should get the highest (most recent) video_ID.

Also, unless you're selecting out more than 1 row, you don't need to use a WHILE loop. You can just do something like this:

PHP Code:

$result mysql_query("SELECT * FROM video ORDER BY video_ID DESC LIMIT 1");
$row mysql_fetch_array($result));
echo 
$row['video_ID'] . " " $row['video_url']; 
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]

Last edited by End User; 12-19-09 at 04:59 PM.
Reply With Quote
  #3 (permalink)  
Old 12-19-09, 03:59 PM
timbaker1991's Avatar
timbaker1991 timbaker1991 is offline
Newbie Coder
 
Join Date: Dec 2009
Location: London, UK
Posts: 38
Thanks: 2
Thanked 3 Times in 3 Posts
that works great thank you! cheers
Reply With Quote
Reply

Bookmarks

Tags
database, mysql, select last


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -5. The time now is 05:14 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.