Forum Topic: PHP mysql select

(183 views • 8 replies)

This topic is 1 page long.

<< < > >>
Resigned

bgraybr

Reply To Post Reply & Quote

Posted at: 7/28/09 06:37 PM

bgraybr DARK LEVEL 09

Sign-Up: 06/30/08

Posts: 2,167

What I'm trying to do is generate a unique id for every user on the site.

I want this script to select the highest id and raise it by one, but when I run the script "$myfetch" and "$genid" are always equal to 1 no matter what.

I know that the mysql query is correct because I used it in phpMyAdmin and it worked fine.

//generate an id
$query = mysql_query("SELECT userid FROM users ORDER BY userid DESC LIMIT 1");
$myfetch = mysql_fetch_array($query);
$genid = (int)$myfetch++;

The following sentence is true. The previous sentence is false. O_o PARADOX!
Linux Users Club | Linketylinklinklinklink...


None

Jon-86

Reply To Post Reply & Quote

Posted at: 7/28/09 06:58 PM

Jon-86 NEUTRAL LEVEL 13

Sign-Up: 01/30/07

Posts: 3,948

As far as I know you can't simply cast and array as an int! Why not use auto increment or simply generate a random number using the system time as the seed value...

PHP Main :: C++ Main :: Java Main :: irc.freenode.net

BBS Signature

Sleeping

Aksumka

Reply To Post Reply & Quote

Posted at: 7/28/09 07:14 PM

Aksumka DARK LEVEL 44

Sign-Up: 08/26/03

Posts: 5,873

Like Jon-86 said, you can set the row userid to auto_increment. Every time a new user is created it will automatically make the next ID one higher than the last.

If you want to get the highest userid, you could use MAX:

$maxID = mysql_query("SELECT MAX(userid) AS maxID FROM users");
$maxIDQ = mysql_fetch_array($maxID);

I'm not an expert in this, but I am using this in a stat generating file on my site.

|| AK || TX ||
NG Archive - The Problems of the Past, Today!

I'm too lazy to upload a non-frosted sig...

BBS Signature

Resigned

bgraybr

Reply To Post Reply & Quote

Posted at: 7/28/09 07:15 PM

bgraybr DARK LEVEL 09

Sign-Up: 06/30/08

Posts: 2,167

At 7/28/09 06:58 PM, Jon-86 wrote: As far as I know you can't simply cast and array as an int! Why not use auto increment or simply generate a random number using the system time as the seed value...

Fixed, I didn't know about auto increment...

The following sentence is true. The previous sentence is false. O_o PARADOX!
Linux Users Club | Linketylinklinklinklink...


None

LostVoices

Reply To Post Reply & Quote

Posted at: 7/28/09 08:42 PM

LostVoices LIGHT LEVEL 07

Sign-Up: 03/09/06

Posts: 1,891

At 7/28/09 07:15 PM, bgraybr wrote:
At 7/28/09 06:58 PM, Jon-86 wrote: As far as I know you can't simply cast and array as an int! Why not use auto increment or simply generate a random number using the system time as the seed value...
Fixed, I didn't know about auto increment...

Just about Database i've used i alwaays have Id Auto increment and Primary Key :)

awww


None

Thomas

Reply To Post Reply & Quote

Posted at: 7/29/09 12:29 AM

Thomas LIGHT LEVEL 13

Sign-Up: 02/14/05

Posts: 2,830

At 7/28/09 07:15 PM, bgraybr wrote: Fixed, I didn't know about auto increment...

Every time you insert something into table, the new entry will have +1 higher than last. Hurrah!

PHP mysql select


None

henke37

Reply To Post Reply & Quote

Posted at: 7/29/09 10:08 AM

henke37 NEUTRAL LEVEL 23

Sign-Up: 09/10/04

Posts: 3,670

While you are learning new things, learn parameterized queries, you sound like the kind of guy who would forgot escaping stuff. With parameterized queries, you will never have to worry about sql injections. However, you need a serious database extension to use those. PDO and mysqli is two extensions for php that does support it. Look for "prepared queries", the name of the implementation technique.

Each time someone abuses hittest, God kills a kitten. Please, learn real collision testing.


None

rockymeet

Reply To Post Reply & Quote

Posted at: 7/30/09 04:38 PM

rockymeet NEUTRAL LEVEL 02

Sign-Up: 02/04/09

Posts: 39

If you want a unique Id for every person . you can use data of field primary key in mysql.
and I hope your problem can be solved thank for join us.
----
FriendyAnil

SEO--SEO

BBS Signature

None

Relish

Reply To Post Reply & Quote

Posted at: 7/30/09 08:01 PM

Relish NEUTRAL LEVEL 06

Sign-Up: 01/22/08

Posts: 781

At 7/30/09 04:38 PM, rockymeet wrote: If you want a unique Id for every person . you can use data of field primary key in mysql.
and I hope your problem can be solved thank for join us.
----
FriendyAnil

Work on your English a bit, and put FriendyAnil or whatever in your signature.

And he's not really "joining us"..


All times are Eastern Standard Time (GMT -5) | Current Time: 02:15 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!