Renumber MySQL UID field

From: koswix15 Jun 2012 15:45
To: Peter (BOUGHTONP) 4 of 29

So simple (fail)

 

I'm helping out a mate who's trying to fix some stuff for his work (their "IT guy" did a runner with the bosses wife :'D ). There was a very nervous pause as the machine froze up for about 20 seconds while it updated the 60-odd thousand rows.

 

THANK YOU PAETER!

From: koswix15 Jun 2012 15:47
To: Matt 5 of 29
Did you log into the right account? I'd normally expect yours and PBs answers to be the other way round. The simple method worked grand!
From: Peter (BOUGHTONP)15 Jun 2012 16:17
To: koswix 6 of 29
I almost gave Matt's answer instead/as well, but something made me say "fuck it" and just give the short one. :)
From: koswix15 Jun 2012 16:19
To: Peter (BOUGHTONP) 7 of 29

Kenny must be tweaking your code.

 

I am now trying to learn some basic SQL super-quick, and then plug it into some PHP. I really should learn to not offer to help people, especially when you don't have a clue what you're doing :S

From: Peter (BOUGHTONP)15 Jun 2012 16:33
To: koswix 8 of 29
Well I don't know if it simplifies things or makes them more complicated, nor how well supported it might be with PHP, but in the CFML world a lot of people have been getting into ORM (e.g. Hibernate), which is supposed to let you avoid writing the majority of SQL, at least for simpler applications.

I don't really do direct database stuff at the moment, so haven't bothered learning much about it, but it might be worth investigating in case it can save you time/effort.
From: koswix15 Jun 2012 16:53
To: Peter (BOUGHTONP) 9 of 29

Probably not worth the effort, as it's only a handful of queries/pages that need fixing.

 

It's basically selecting 6 random rows from the database and then printing them out one at a time at various points on a webpage.

 

I think I've almost got it sorted, not knowing what you're doing really makes it quite interesting though :D

From: Peter (BOUGHTONP)15 Jun 2012 17:14
To: koswix 10 of 29
Heh, that's almost what I've been working on recently, though my stuff selects random files from a directory instead - much nicer to work with. :)
From: Matt15 Jun 2012 18:04
To: koswix 11 of 29
Disadvantage of Pete's is that if it's an auto-increment it will continue from the old maximum value, not from the next available one.

If you had records:

10 Hello,
11 World

And you used Pete's code to change them to:

1 Hello
2 World

The next value you insert into the table will have the UID of 11 and not 3.
From: koswix15 Jun 2012 18:08
To: Matt 12 of 29
I noticed in PHPMyAdmin that you can reset the auto-increment counter, would taht solve that? Or would it make things start at 0 again :S
From: Matt15 Jun 2012 18:10
To: koswix 13 of 29
Depends if you can reset it or change it to a specific value.
From: koswix15 Jun 2012 18:12
To: Matt 14 of 29
It's change, not reset. I've changed it to 1 after the current last entry, hopefully that's it sorted.
From: koswix18 Jun 2012 14:38
To: ALL15 of 29

Anyone want to help me with this? And by help, i mean tell me exactly what to type :$

 

I have a table with around 100 entries, each one is a vetted customer testimonial type thing and the data isn't likely to change/grow much.

 

What I need is:

 

Select 6 of them at random, then they'll be displayed at various points on a page.

 


I've got an SQL query working for it (I know it's not the most efficient/fastest way of doing it, but it seemed the most straightforward way to get 6 unique random rows):

 

SELECT text FROM `content` ORDER BY RAND() LIMIT 0,6

 


Now how do i get that into an array so I can print each row result individually? All the stuff online seems to be about finding individual rows and adding them to an array, not finding 6 rows and adding each row to an array.

 


Or should I bite the bullet and learn some PHP so I can do it myself? Just sees like a waste as I'm unlikely to ever need it again after this :$

From: Peter (BOUGHTONP)18 Jun 2012 15:15
To: koswix 16 of 29
Learn some PHP - you would have also said you're unlikely to ever need it before finding out about this, right?

If "all the stuff online" is telling you how to deal with single rows, then you're not looking at all the stuff (or are misunderstanding it, thus learning a bit would be beneficial).

In any case, Stack Overflow has this question asked/answered several times. Like here.
From: koswix18 Jun 2012 16:02
To: Peter (BOUGHTONP) 17 of 29
But why learn when I can get you to find the answer for me?! (kiss)
From: koswix18 Jun 2012 23:00
To: Peter (BOUGHTONP) 18 of 29
Your solution doesn't work :(
From: Drew (X3N0PH0N)18 Jun 2012 23:05
To: koswix 19 of 29
You must be doing it wrong.

code:
$query = mysql_query("SELECT text FROM `content` ORDER BY RAND() LIMIT 0,6");
$hooray = array();
while($result = mysql_fetch_array($query)) {
    $hooray[] = $result;
}
 
print_r($hooray);


(last line just prints the array)
From: koswix18 Jun 2012 23:16
To: Drew (X3N0PH0N) 20 of 29

Ah-hah. The problem is with how I'm trying to print the array elements.

 

If I use "print" it just outputs "Array". print_r prints the content, but does it like this:

 

Array ( [0] => The service was excellent. [text] => The service was excellent. )

 

How on earth do i just get the bit of text I need out of it?! This shit was so easy in QBASIC :(

From: Drew (X3N0PH0N)18 Jun 2012 23:18
To: koswix 21 of 29
(just a note: that will result in an array of arrays. i.e. $hooray will be an array containing arrays containing the result elements. So the first element of the first result will be at $hooray[0][0], second element of the first result at $hooray[0][1] and first element of the second result at $hooray[1][0] and so on)
From: Peter (BOUGHTONP)18 Jun 2012 23:19
To: koswix 22 of 29
It's not my solution, but it works perfectly.
From: Drew (X3N0PH0N)18 Jun 2012 23:19
To: koswix 23 of 29
As above. I think fetch_array produces both an indexed and an associative array (or one which acts as both, I dunno (and they might not be the right words)).

So you can do either:

$hooray[0]['text'] or $hooray[0][0]

They will point to the same thing.