简体   繁体   English

如何从php脚本中增加sql DB

[英]How to increment a sql DB from php script

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. 我有一个SQL DB,它只是一个表,我想从我的iphone中增加一行,只是为了测试与服务器的通信。

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. 到目前为止,我已经创建了如上所述的sql db。 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脚本增加...到目前为止,对于脚本,我已经完成了基本的操作来检查数据库连接,所以它看起来像像这样的小东西..

<?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 :) 任何帮助将不胜感激..希望一旦我帮助搞清楚这一点,我将能够继续前进并做更复杂的事情:)

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 如果您想拥有多行,并且id字段不断增加,请使用

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. MySQL不会真正将字段设置为0,而是设置为新的增加值。

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. 此外,对于每个可用NULL或具有已定义值集的列,您也可以跳过它们,它将分别自动分配NULL和默认值。

The mysql connection: mysql连接:

<?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: 那么INSERT:

<?php
mysql_query("INSERT INTO workers (id) VALUES (null)");
?>

Hope it helps! 希望能帮助到你! =D = d

AUTO_INCREMENT means each successive row you insert has an incrementally higher id. AUTO_INCREMENT意味着您插入的每个连续行都具有递增的更高ID。 All you should need is a primary key on the id column. 您应该需要的只是id列的主键。

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. 如果没有行,这将插入一行,但如果您已经插入了一行,则增加id。

Try: 尝试:

INSERT INTO `workers` VALUES (NULL) 

or 要么

INSERT INTO `workers` (id) VALUES (NULL) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM