Reputation: 5510
I have a SQL DB that is just one table with one row that I would like to increment from my iphone, just to test out communication with a server.
I have done this with the phone before but I have never done the stuff on there server side and would like to learn.
so far I have created the sql db as said above. it is pretty much just this.
CREATE TABLE workers
(
id SMALLINT NOT NULL AUTO_INCREMENT
)
that could be slightly wrong.. it took me a couple of times to get it to work on the server and I am only writing it from memory now.
However now I would like to figure out how to just make it increment from a php script on my server... so far for the script I have done the basic stuff you do to get it to check for the DB connectivity so it looks a little something like this..
<?PHP
$con = mysql_connect("db.url.com","pass","username");
if (!$con){die('Could not connect: ' . mysql_error());}
mysql_select_db("db", $con);
?>
I am not sure what to do now.. I would guess its some type of insert statment, but im not sure of this as I am using Auto Increment on that row...
any help would be greatly appreciated.. hopefully once I have had help figuring this out I will be able to move on and do more complex things myself :)
Upvotes: 1
Views: 568
Reputation: 604
The mysql connection:
<?php
$hostname = "yourhostname";
$database = "yourdatabase";
$username = "youusername";
$password = "yourpasword@";
$conn = mysql_pconnect($hostname,$username,$password) or die();
mysql_select_db($database,$conn) or die(“ERROR ”.mysql_error());
?>
Then the INSERT:
<?php
mysql_query("INSERT INTO workers (id) VALUES (null)");
?>
Hope it helps! =D
Upvotes: 1
Reputation: 65342
If you want to have just one row in the table, with a value increasing on every call, do
CREATE TABLE workers (id INT);
INSERT INTO workers SET id=0; -- or whatever start value you want
Then for each increment run
UPDATE workers SET id=id+1;
If you want to have more than one row, with ever increasing id
field, use
CREATE TABLE workers (id INT AUTO_INCREMENT PRIMARY KEY); -- and other needed cols
And for every row do
INSERT INTO workers SET id=0; -- again and other fields
MySQL will not really set the field to 0, but to a new increased value.
Upvotes: 2
Reputation: 587
Try:
INSERT INTO `workers` VALUES (NULL)
or
INSERT INTO `workers` (id) VALUES (NULL)
Upvotes: 0
Reputation: 506
AUTO_INCREMENT means each successive row you insert has an incrementally higher id. All you should need is a primary key on the id column.
INSERT INTO `workers` (`id`) VALUES (1) ON DUPLICATE KEY UPDATE `id` = `id` + 1
This will insert a row if there are no rows, but if you've already inserted a row, increments the id.
Upvotes: 0
Reputation: 8578
You can just ignore that column. For example for this table structure
CREATE TABLE workers
(
id SMALLINT NOT NULL AUTO_INCREMENT,
name varchar(30) NOT NULL
)
you would just do:
mysql_query("INSERT INTO workers(name) VALUES('John Peters')");
and it will add that column automatically. Moreover, for every column that has NULL available or has a defined value set, you can skip them too and it would automatically assign NULL and the default value respectively.
Upvotes: 1