SRL Archive - Web Development

php / mysql help


grats

2011-10-19 19:07:58

in my query I have "SELECT * FROM table";

all works fine

it orders it in a specific way which is the first row in table -> last row in table

I'd like to order it the exact opposite way, I am unsure how to do this..

I know

[PHP]$query="SELECT * FROM table ORDER BY column DESC"; [/PHP]

but the problem is, I don't want it ordered by any column, I want it ordered by the opposite of when it was ordered
is there a special thing to use?


I've googled all over and am unable to find it

Nava2

2011-10-19 19:09:44

http://www.w3schools.com/php/func_array_reverse.asp ?

grats

2011-10-19 19:11:35

From: Nava2
http://www.w3schools.com/php/func_array_reverse.asp ?


woo that looks like it would maybe work, where exactly do I put that?

Nava2

2011-10-19 19:14:41

Better: http://www.sql-tutorial.com/sql-order-by-sql-tutorial/

Also, your query should work as you intend.. unless you just want to rearrange the row order by removing the DESC keyword.

grats

2011-10-19 19:17:00

From: Nava2
Better: http://www.sql-tutorial.com/sql-order-by-sql-tutorial/

Also, your query should work as you intend.. unless you just want to rearrange the row order by removing the DESC keyword.


well that one is all the ORDER BY stuff I was saying, I don't have a column I want to order by, it's just I want them ordered by the opposite of when they were added

this is my code:
sry had to screenshot because I can't copy paste through my remote connection


so basically in the gallery, I'd like to order it by

this image was just added

this image was added a day ago

this image was added a week ago

Nava2

2011-10-19 19:22:47

Perhaps you need to implement IDs into your database then. When a picture is added, make the ID the last+1 then just do that for each image. Then you can just do:

SELECT * FROM gallery ORDER BY UPID DESC

Would make your life easier, and then you can also reference it later by just the UPID rather than needing the user etc. It is pretty standard practice, really.

grats

2011-10-19 19:24:21

oh yea that would be really easy, then just reverse by the ID #

how do I make it do that? sorry I don't really know any of this, just learning by doing some of these types of things



edit
basically I just need to know how to do the +1 thing everytime to that ID column, I'm not sure how to do anything like that
I understand how that would work a lot better though

Nava2

2011-10-19 19:36:16

From: grats
oh yea that would be really easy, then just reverse by the ID #

how do I make it do that? sorry I don't really know any of this, just learning by doing some of these types of things



edit
basically I just need to know how to do the +1 thing everytime to that ID column, I'm not sure how to do anything like that
I understand how that would work a lot better though


I'm not huge on web development, and there is a probably a better way.. but having a file on the server that stores settings and just importing the number/ID each time would work. Oh, and I suggest using IDs that are alphanumerical.. and case sensitive. It increases your entropy of IDs much more. Ex 000000 -> ZZZZZZ rather than 000000->999999.. Or at least use hex lol. Either way, you will have to figure that out as my webdev knowledge is near zero.

grats

2011-10-19 19:39:23

yea idk how to do any of that lol!

my friend and I will figure it out somehow, and yea I think numbers only would be fine, if I give it like 10 characters of space.. I doubt I'll have over a trillion images in the gallery tbh

doubt I'll even get over 100k lol



thanks for your input though, hopefully someone else knows how to do it maybe

sirlaughsalot

2011-10-19 19:59:54

Do you have phpmyadmin or anything? Because there you can use a UI to add it, but to do it yourself, just run something like.


ALTER TABLE `tablename` ADD COLUMN `id` int(10) unsigned NOT NULL
ALTER TABLE `tablename` ADD PRIMARY KEY (id)

I think something like that would work

grats

2011-10-19 20:14:27

From: sirlaughsalot
Do you have phpmyadmin or anything? Because there you can use a UI to add it, but to do it yourself, just run something like.


ALTER TABLE `tablename` ADD COLUMN `id` int(10) unsigned NOT NULL
ALTER TABLE `tablename` ADD PRIMARY KEY (id)

I think something like that would work


no just been doing all the mysql stuff manually, pretty simple stuff to do, kind of like cisco router prompt stuff

that primary key though is something new, is that something that automates the numbers by itself?

trying it out now, just added the id column

Harry

2011-10-19 20:17:15

From: sirlaughsalot
Do you have phpmyadmin or anything? Because there you can use a UI to add it, but to do it yourself, just run something like.

Installing phpmyadmin is a great way to get your server hacked very fast and easily. I'd highly suggest against using it.

sirlaughsalot

2011-10-19 20:18:44

From: grats
no just been doing all the mysql stuff manually, pretty simple stuff to do, kind of like cisco router prompt stuff

that primary key though is something new, is that something that automates the numbers by itself?

trying it out now, just added the id column


I apologize, I forgot to add the AUTO INCREMENT.


ALTER TABLE `tablename` MODIFY COLUMN `id` AUTO INCREMENT

Edit:
Just fyi I am not too familiar with modifying databases with queries, so these may be a little off.

grats

2011-10-19 20:22:45

From: sirlaughsalot
I apologize, I forgot to add the AUTO INCREMENT.


ALTER TABLE `tablename` MODIFY COLUMN `id` AUTO INCREMENT

Edit:
Just fyi I am not too familiar with modifying databases with queries, so these may be a little off.


oh that must be the problem I was having then lol

I'm trying to search for the correct command on google, syntax error with auto increment

From: Harry
Installing phpmyadmin is a great way to get your server hacked very fast and easily. I'd highly suggest against using it.


Yea I won't ever be touching it… I work with cmd stuff all day, mysql is no prob to do manually.

grats

2011-10-19 20:30:37

From: sirlaughsalot
I apologize, I forgot to add the AUTO INCREMENT.


ALTER TABLE `tablename` MODIFY COLUMN `id` AUTO INCREMENT

Edit:
Just fyi I am not too familiar with modifying databases with queries, so these may be a little off.


unable to figure out


I added the auto increment correctly it said.. but it doesn't let me add more than 1 thing to the table through my php command




yea

ALTER TABLE name ADD id int(10) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY;

that line worked, gotta figure this adding thing now



ah AWESOME! got it

thanks a bunch sirlaughsalot!!!!

sirlaughsalot

2011-10-19 20:35:15

From: grats
oh that must be the problem I was having then lol

I'm trying to search for the correct command on google, syntax error with auto increment


ALTER TABLE  `tablename` CHANGE  `id`  `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT

Try that

Edit:

Nevermind, feel free to add me on MSN if you need any more help.

grats

2011-10-19 21:01:08

From: sirlaughsalot
ALTER TABLE  `tablename` CHANGE  `id`  `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT

Try that

Edit:

Nevermind, feel free to add me on MSN if you need any more help.


added, thanks for the help :P

been messing with a lot of php stuff this past week so I'll probably need you again sooner or later

Floor66

2011-10-20 18:03:47

id would work, timestamp would work, you name it.

I always make the ID the primary key as well.