Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I don't know how to COUNT a column named id. I tried

mysql_query("INSERT INTO `servers` (`user_id`, `ip`, `port`, `banner`, `disabled`, `vip`,`premium`, `name`, `status`, `votifier_key`, `votifier_port`, `country`)
             VALUES ('$session_user_id', '$ip', '$port', 's=.'id'.back', '$disabled', 0,'false', '$name', '1', '$votifier', '$votPort', '$country')");

But it's not working, because I couldn't get id. Can someone help?

share|improve this question
2  
1 The mysql_* functions are deprecated and will be removed in future releases of PHP. Use mysqli or PDO instead. 2 Your code is vulnerable to SQL injection. Same fix. – michaelb958 5 hours ago
1  
Are you trying to count or insert? Counting would be something like: SELECT COUNT(*) FROM table. Please describe what you're trying to do in more detail. – showdev 5 hours ago
Is your question about the part of the insert with 's=.'id'.back'? What is that supposed to do? – Barmar 5 hours ago
Im trying to COUNT and INSERT, like I wanna do SELECT COUNt('id') FROM servers as serverid,INSERT INTO servers (user_id, ip, port, banner, disabled, vip,premium, name, status, votifier_key, votifier_port, country) VALUES ('$session_user_id', '$ip', '$port', 's=.'serverid'.back', '$disabled', 0,'false', '$name', '1', '$votifier', '$votPort', '$country')"); – Tiago Castro 5 hours ago
Barmar, 'id' is a collum that I count..I want to count and discovery the value and input this value. – Tiago Castro 5 hours ago
show 10 more comments

1 Answer

You need to use INSERT ... SELECT request.

Suppose, we have an empty table test:

     test
|----+-------|
| id | value |
|----+-------|

CREATE TABLE IF NOT EXISTS `test` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `value` int(10) unsigned DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

When we run this SQL request:

INSERT INTO test (value) SELECT COUNT(*) FROM `test`

we shall sequentially get test filling up with data:

| id | value |
|----+-------|
| 1  | 0     |
| 2  | 1     |
| 3  | 2     |

Use this approach for your table, and you'll get what you need.

share|improve this answer
Ohh Thanks man.. – Tiago Castro 4 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.