Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-06-2012, 08:50 PM   PM User | #1
fus10n
New Coder

 
Join Date: Oct 2010
Posts: 37
Thanks: 3
Thanked 0 Times in 0 Posts
fus10n is an unknown quantity at this point
Delete rows with smallest id value

Okay,

How would I go about deleting say 10 rows from mysql db where that have the least id value, or the highest id value...or just the first 10 or last 10 rows...thanks in advance
fus10n is offline   Reply With Quote
Old 04-06-2012, 08:55 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,046
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
What is the name of your MySQL table and some column names?
mlseim is offline   Reply With Quote
Old 04-06-2012, 08:56 PM   PM User | #3
fus10n
New Coder

 
Join Date: Oct 2010
Posts: 37
Thanks: 3
Thanked 0 Times in 0 Posts
fus10n is an unknown quantity at this point
Table Name = urls

Columns would be id, url, comment_submitted, used
fus10n is offline   Reply With Quote
Old 04-07-2012, 04:15 AM   PM User | #4
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,046
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Untested script ... not sure if this will work correctly or not, but it might be enough to get you started ...

PHP Code:
<?php

//choices
// 1= the least 10
// 2= the most 10
// 3= 10 from the top and 10 from the bottom
// pick the choice.
$choice=1;

// connect to your database here

if($choice==1){
$query="SELECT * FROM urls ORDER BY id DESC LIMIT 0,10";
}
if(
$choice==2){
$query="SELECT * FROM urls ORDER BY id ASC LIMIT 0,10";
}
if(
$choice==3){
$query="SELECT * FROM urls ORDER BY id DESC LIMIT 0,10 UNION SELECT * FROM urls ORDER BY id ASC LIMIT 0,10 UNION ORDER BY id ASC";
}
 
// display results of query here

?>
mlseim is offline   Reply With Quote
Old 04-07-2012, 02:09 PM   PM User | #5
fus10n
New Coder

 
Join Date: Oct 2010
Posts: 37
Thanks: 3
Thanked 0 Times in 0 Posts
fus10n is an unknown quantity at this point
appreciate the help, but all of them choice 1/2 delete the last 10 and choice 3 doesnt do anything.
fus10n is offline   Reply With Quote
Old 04-07-2012, 02:20 PM   PM User | #6
fus10n
New Coder

 
Join Date: Oct 2010
Posts: 37
Thanks: 3
Thanked 0 Times in 0 Posts
fus10n is an unknown quantity at this point
Okay, This is what it echos: Resource id #2, meanwhile the smallest id in the table is 40392


PHP Code:
<?php
$con 
mysql_connect("localhost","*****","*****");
mysql_select_db("dubstep3_project1");
if (!
$con)
  {
  die(
'Could not connect: ' mysql_error());
  }


//choices 
// 1= the least 10 
// 2= the most 10 
// 3= 10 from the top and 10 from the bottom 
// pick the choice. 
$choice=1

// connect to your database here 

if($choice==1){ 
$querymysql_query("SELECT * FROM urls ORDER BY id DESC LIMIT 0,10"); 

if(
$choice==2){ 
$querymysql_query("SELECT * FROM urls ORDER BY id ASC LIMIT 0,10"); 

if(
$choice==3){ 
$querymysql_query("SELECT * FROM urls ORDER BY id DESC LIMIT 0,10 UNION SELECT * FROM urls ORDER BY id ASC LIMIT 0,10 UNION ORDER BY id ASC"); 

  
// display results of query here 
echo $query;   







?>
fus10n is offline   Reply With Quote
Old 04-07-2012, 02:46 PM   PM User | #7
fus10n
New Coder

 
Join Date: Oct 2010
Posts: 37
Thanks: 3
Thanked 0 Times in 0 Posts
fus10n is an unknown quantity at this point
I changed it again and now it echos: ArrayArrayArrayArrayArrayArrayArrayArrayArrayArray

PHP Code:
<?php
$con 
mysql_connect("localhost","******","******");
mysql_select_db("dubstep3_project1");
if (!
$con)
  {
  die(
'Could not connect: ' mysql_error());
  }


//choices 
// 1= the least 10 
// 2= the most 10 
// 3= 10 from the top and 10 from the bottom 
// pick the choice. 
$choice=1

// connect to your database here 

if($choice==1){ 
$query1mysql_query("SELECT * FROM urls ORDER BY id DESC LIMIT 0,10"); 

if(
$choice==2){ 
$query2mysql_query("SELECT * FROM urls ORDER BY id ASC LIMIT 0,10"); 

if(
$choice==3){ 
$query3mysql_query("SELECT * FROM urls ORDER BY id DESC LIMIT 0,10 UNION SELECT * FROM urls ORDER BY id ASC LIMIT 0,10 UNION ORDER BY id ASC"); 

while (
$res mysql_fetch_array($query1))

  {
// display results of query here 
echo $res;   

}





?>
fus10n is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:42 AM.


Advertisement
Log in to turn off these ads.