Jump to content

How to fix MySQL Count Rows?

- - - - -

  • Please log in to reply
3 replies to this topic

#1
hetra

hetra

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 298 posts
  • Location:Australia
  • Programming Language:C, C++, PHP, Python, Delphi/Object Pascal, Assembly
  • Learning:Python, Assembly
Hello all,

I'm finishing off my blog commenting system on my website. I want the comment to be posted with a cid (Comment IDentification). Basically, when I try and make in AUTO_INCREMENT in PHPMyAdmin, it says:

#1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key

I would try to do it in PHP by adding one to the result of

SELECT COUNT(*) FROM comments

but it would fail as it's an associative array.

Thanks.
Jack
Creator, Owner, Webmaster
Brezerd.net

#2
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
What about this?:
SELECT MAX(cid)+1 FROM ...
Or this?:
INSERT INTO table_name (id, cid, comment_text) SELECT MAX(id)+1, MAX(cid)+1, 'the text for the comment...' FROM table_name 
The latter is similar to what I actually used for a website, for registering users.

#3
hetra

hetra

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 298 posts
  • Location:Australia
  • Programming Language:C, C++, PHP, Python, Delphi/Object Pascal, Assembly
  • Learning:Python, Assembly
Thanks, the latter one did work fine. I don't usually pry into that much SQL syntax so thanks for the help.
Jack
Creator, Owner, Webmaster
Brezerd.net

#4
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,298 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
Sorry, but I don't understand the difference between the id and the cid column in this table, as they seem to get the same value on all rows if you start with an empty table... why not just auto-increment the cid and skip the id column?
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users