TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-23-2008, 12:03 AM   #1 (permalink)
The Frequenter
 
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
Aaron is on a distinguished road
Default Is this a good query?

PHP Code:
CREATE TABLE `users` (
`
idSMALLINT UNSIGNED NOT NULL AUTO_INCREMENT ,
`
nameVARCHAR30 NOT NULL ,
`
passwordVARCHAR30 NOT NULL ,
PRIMARY KEY ( `id` )
); 
is that a good query for a member table?
Send a message via MSN to Aaron
Aaron is offline  
Reply With Quote
Old 02-23-2008, 12:23 AM   #2 (permalink)
The Contributor
 
DeMo's Avatar
 
Join Date: Jan 2008
Location: Brazil
Posts: 77
Thanks: 14
DeMo is on a distinguished road
Default

Well.. that depends on what you're building. If for this system you need only the user's name and password then it's ok.

About the password, how do you plan to store them?
If you're gonna store flat passwords (kinda insecure) 30 characters are ok, but if you're going to store the md5 hash you're gonna need 32 chars. For a sha-1 hash you need 40 chars.
Send a message via ICQ to DeMo Send a message via MSN to DeMo Send a message via Skype™ to DeMo
DeMo is offline  
Reply With Quote
The Following User Says Thank You to DeMo For This Useful Post:
Aaron (02-23-2008)
Old 02-23-2008, 01:41 AM   #3 (permalink)
The Acquainted
 
Join Date: Nov 2007
Posts: 154
Thanks: 31
SOCK is on a distinguished road
Default

I agree with DeMo, you need to consider the `password` field and how it relates to what hash you're storing there.

In addition, make sure you add a UNIQUE index on the `name` field.

SMALLINT is probably enough for your application, but using INT isn't going to hurt anything.
__________________
I reject your reality, and substitute my own.
SOCK is offline  
Reply With Quote
The Following User Says Thank You to SOCK For This Useful Post:
Aaron (02-23-2008)
Old 02-23-2008, 02:43 AM   #4 (permalink)
The Frequenter
 
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
Aaron is on a distinguished road
Default

Thanks for your help. I didn't think that more than 4 people would have an account on this, so I just made it a smallint.

Last edited by Aaron : 02-23-2008 at 07:49 PM.
Send a message via MSN to Aaron
Aaron is offline  
Reply With Quote
Old 02-23-2008, 04:59 AM   #5 (permalink)
The Acquainted
 
Join Date: Nov 2007
Posts: 154
Thanks: 31
SOCK is on a distinguished road
Default

Good call on the ALTER statement to create a UNIQUE index. In cases like this where you want to avoid users having the same username, the easiest way to implement it (and have the benefits of using an index) is with UNIQUE.

Quote:
Originally Posted by Aaron View Post
Thanks for your help. I didn't think that more than 4 people would have an account on this, so I just made it a smallint.

Well, that will certainly hold 4 accounts.

TINYINT (unsigned) is up to 255.
SMALLINT (unsigned) will run up to 65,535.
INT (unsigned) is up to 4,294,967,295 (yes, that's trillion).

In your case, especially for test tables or small user groups, TINYINT(2) UNSIGNED (to 99) will usually suffice. Just one byte per value.

MySQL Manual : Data Types | Numeric types
__________________
I reject your reality, and substitute my own.
SOCK is offline  
Reply With Quote
Old 02-23-2008, 06:20 AM   #6 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by SOCK View Post
TINYINT(2) UNSIGNED (to 99)
TINYINT(2) UNSIGNED still has the normal range of values (0-255) of plain jane TINYINT UNSIGNED. The (2) is only a padding value if the column is declared with ZEROFILL.

To put clearer, if you try to insert a value of 100 into a TINYINT(2) column then it'll work fine. If you try to insert 256, then MySQL will throw back a warning "Out of range value adjusted for column 'id' at row 1" and adjust the value to 255.
Salathe is offline  
Reply With Quote
The Following 2 Users Say Thank You to Salathe For This Useful Post:
DeMo (02-23-2008), SOCK (02-23-2008)
Old 02-23-2008, 06:32 AM   #7 (permalink)
The Acquainted
 
Join Date: Nov 2007
Posts: 154
Thanks: 31
SOCK is on a distinguished road
Default

salathe> Thanks for the clarification. I guess I should read the links I post completely, eh?
__________________
I reject your reality, and substitute my own.
SOCK is offline  
Reply With Quote
Old 02-23-2008, 07:52 PM   #8 (permalink)
The Frequenter
 
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
Aaron is on a distinguished road
Default

Why doesn't this work?
PHP Code:
<?php
@require_once("config.php");

//create a table
mysql_query(CREATE TABLE `testusers
    (
    `
idTINYINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE,
    `
nameVARCHAR(40NOT NULL,
    `
passwordVARCHAR(40NOT NULL,
    
PRIMARY KEY(`id`)
    );
) || die(
mysql_error());
?>
Send a message via MSN to Aaron
Aaron is offline  
Reply With Quote
Old 02-23-2008, 08:09 PM   #9 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Your SQL isn't wrapped in string delimiters (single quotes).
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
Aaron (02-23-2008)
Old 02-23-2008, 08:33 PM   #10 (permalink)
The Frequenter
 
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
Aaron is on a distinguished road
Default

Okay...
How do I connect to the database when my username doesn't have a password?
Send a message via MSN to Aaron
Aaron is offline  
Reply With Quote
Old 02-23-2008, 09:31 PM   #11 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

Just use "" in place of the password in mysql_connect()

Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
The Following User Says Thank You to Alan @ CIT For This Useful Post:
Aaron (02-24-2008)
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

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


All times are GMT. The time now is 07:21 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design