On 2005-04-01, Japhy <japhyrider2005@yahoo.com> wrote:[color=blue]
> Hello. I am writing my first web based PHP/MYsql application.[/color]
Welcome!
[color=blue]
> I have used the following code to retrieve the hightest value in a
> field :
>
><?php
> mysql_connect("localhost", "root", "") or[/color]
It's best to not connect to your database as root. Much better
to create a test_xxx table or an additional database user and connect as
that.
[color=blue]
> die("Could not connect: " . mysql_error());
> mysql_select_db("MYdb");
>
> $result = mysql_query("SELECT cont_num FROM findates");
>
> while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
> printf("cont_num: %s", $row[0]);
> }[/color]
The best way to find the maximum value is to use a query like this:
SELECT MAX(cont_num) FROM findates
[color=blue]
>
> mysql_free_result($result);
> ?>
>
> I have verified that it works with a print. I now want to increment
> the $result by 1 to use as a new key. Can I do something like :
>
> MYdb.MyTable.cont_num = $result+1;[/color]
You will need to run a query such as the following:
UPDATE MyTable SET cont_num = $result + 1
However, since you mention you want to use it as a key, I'm not sure
what you're asking is really what you want. I think what you are
interested in is what mysql calls an AUTO_INCREMENT field... see here:
http://dev.mysql.com/doc/mysql/en/ex...increment.html
[color=blue]
> I have tried various formats with no luck (quotes, +=, parens, etc).
> I know there must be documentation on this, but so far no luck making
> sense.[/color]
Start here... you'll save yourself a lot of headaches with some reading
first.
http://www.php.net http://dev.mysql.com/doc/mysql/en/index.html http://www.devshed.com/c/b/PHP/ http://www.zend.com/zend/tut/index.php
If you want a book, there's some listed here...
http://www.php.net/books.php
Have fun!
-philip