php mysql problems

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
cheatboy00
Forum Contributor
Posts: 151
Joined: Sat Jun 29, 2002 10:36 am
Location: canada
Contact:

php mysql problems

Post by cheatboy00 »

I'm creating a script that allows you to become a member of my site. the problem is my id colum doesnt count the number of members. now i'll bet i'm the person with the least amount of experience in php and mysql. but ould you help me out. my script looks like this:

$a = mysql_query("SELECT MAX(id) FROM users");
$c .= $a +1;

(note: $c is the variable that goes into the id coloum)
I've tried many different ways but none of them work they dont count each person, like sometimes it'll be 0 when i go look at the table or 1 but it doesnt go futher than that.

i know theres other ways to do it i just dont know how, theres somethign to do with the auto_increment but i got no idea how to do that....

any help you can give would be greatly apreciated


note: oops i just realized that this should be in the database forum, oh well...
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

Code: Select all

CREATE TABLE users (
 fid TINYINT not null AUTO_INCREMENT, 
 username varchar(20) UNIQUE NOT NULL,
 password varchar(20) NOT NULL,
 email varchar(20) NOT NULL,
 aim varchar(16) NOT NULL,
 profile varchar(255) NOT NULL,
 PRIMARY KEY (fid)

);
is what i used for an old member table i was making... and fid would go up a number each time something was inserted, and you didnt have to specify a value.

so then you could do that and then try...

$query = mysql_query("SELECT * FROM users ORDER BY fid DESC LIMIT 1");

and that would return the whole row of the latest user
User avatar
QWERTY
Forum Newbie
Posts: 20
Joined: Sat Jun 29, 2002 10:57 am
Location: Slovenia

...

Post by QWERTY »

Code: Select all

$a = mysql_query("SELECT COUNT(id) FROM users"); 
$c .= $a +1;
I think this should work...:D

But this is realy not the best way to work with MySQL. Why don't you try a "program" (script) like phpMyAdmin.
Than you can simply add auto_increment on id field in your table, and you won't have to run extra querys for id field...

BTW: If you are interested in learning SQL, try this link:
http://www.w3schools.com/sql/
User avatar
cheatboy00
Forum Contributor
Posts: 151
Joined: Sat Jun 29, 2002 10:36 am
Location: canada
Contact:

Post by cheatboy00 »

thats where i learned what little mysql i know i think i should go back and see what i missed...

to bad i cant try any of these ideas out my host is down... this amazese me the host that i use like never goes down i've heard of it going down but i guess this is the first time in a long time it acutally went down meah

thanks for all the help
User avatar
QWERTY
Forum Newbie
Posts: 20
Joined: Sat Jun 29, 2002 10:57 am
Location: Slovenia

...

Post by QWERTY »

O, and why don't you write SQL querys like this:

Code: Select all

$a = mysql_query("SELECT COUNT(id) FROM users") or die ("Error:<br />" . mysql_error() ); 
$c .= $a +1;
It's better becaose you get error message if there is an error in your syntax...
User avatar
cheatboy00
Forum Contributor
Posts: 151
Joined: Sat Jun 29, 2002 10:36 am
Location: canada
Contact:

Post by cheatboy00 »

i see that alot, i was going through some things on php.net in their manul and i see they have an "or die" after every line they produce whats with that. is that to tell where you went wrong in the code?
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

personally...i only use or die() on longer statements...

also personally, i use phpmyadmin just for creating tables and overviewing them. and i let my scripts handle the information.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

ok or die()

well..

if i have something like mysql_query() and it tries to query a database that doesnt exist, or put something in a row that doesn't exist...or just plain can't connect or some other error... or die() will end the script right then and there and print whatever is in die( <here> )
User avatar
cheatboy00
Forum Contributor
Posts: 151
Joined: Sat Jun 29, 2002 10:36 am
Location: canada
Contact:

Post by cheatboy00 »

well i do have one.

its on my online control panel from my host. the control panel they give you is on a different server which is cool that way i can still work somewhat on my site if they go down.

so i just went there and did your version of the answer (hob goblin). and i inserted a few new rows and it worked thanks
User avatar
QWERTY
Forum Newbie
Posts: 20
Joined: Sat Jun 29, 2002 10:57 am
Location: Slovenia

...

Post by QWERTY »

see that alot, i was going through some things on php.net in their manul and i see they have an "or die" after every line they produce whats with that. is that to tell where you went wrong in the code?
Yes, it is to tell where you went wrong...:D
also personally, i use phpmyadmin just for creating tables and overviewing them. and i let my scripts handle the information.
yes, for information, but for creating databases nad tables I use phpMyAdmin... but for inserting, updating, the information in these tables I use SQL. Of course I won't insert informations manual... waste of time ...:D
Post Reply