1

我有一个 SQL DB,它只是一个表,其中一行我想从我的 iphone 中增加,只是为了测试与服务器的通信。

我以前用手机做过这个,但我从来没有在服务器端做过这些东西,我想学习。

到目前为止,我已经创建了如上所述的 sql 数据库。差不多就是这样。

CREATE TABLE workers
(
id SMALLINT NOT NULL AUTO_INCREMENT
)

这可能有点错误..我花了几次才让它在服务器上工作,我现在只是从内存中写出来的。

但是现在我想弄清楚如何让它从我服务器上的 php 脚本中增加......到目前为止,对于脚本,我已经完成了基本的工作来让它检查数据库连接,所以它看起来像这样的小东西。。

<?PHP
$con = mysql_connect("db.url.com","pass","username");
if (!$con){die('Could not connect: ' . mysql_error());}
mysql_select_db("db", $con);

?>

我不知道现在该怎么办..我猜它是某种类型的插入语句,但我不确定这一点,因为我在那行使用自动增量...

任何帮助将不胜感激..希望一旦我得到帮助解决这个问题,我将能够继续前进并自己做更复杂的事情:)

4

5 回答 5

2

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.

于 2012-06-02T23:24:09.903 回答
1

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

于 2012-06-02T23:24:45.573 回答
1

您可以忽略该列。例如对于这个表结构

CREATE TABLE workers
(
id SMALLINT NOT NULL AUTO_INCREMENT,
name varchar(30) NOT NULL
)

你会这样做:

mysql_query("INSERT INTO workers(name) VALUES('John Peters')");

它会自动添加该列。此外,对于每个具有 NULL 可用或已定义值集的列,您也可以跳过它们,它会自动分别分配 NULL 和默认值。

于 2012-06-02T23:21:42.897 回答
0

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.

于 2012-06-02T23:22:18.953 回答
0

Try:

INSERT INTO `workers` VALUES (NULL) 

or

INSERT INTO `workers` (id) VALUES (NULL) 
于 2012-06-02T23:23:31.923 回答