Connecting Tech Pros Worldwide Forums | Help | Site Map

Beginner needs help

Japhy
Guest
 
Posts: n/a
#1: Jul 17 '05
Hello. I am writing my first web based PHP/MYsql application.
I have used the following code to retrieve the hightest value in a
field :

<?php
mysql_connect("localhost", "root", "") or
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]);
}

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;

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.

Any help appreciated!
Japhy


ZeldorBlat
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Beginner needs help


I'm not sure how that get's you the max value. Try writinng your query
as "SELECT max(cont_num) FROM finddates". Then you'll just get one row
with the max.

If you want to change data in the database, you need to write and
execute another query. If adding a new row, the query would be
something like:

INSERT finddates (cont_num) values (NEW_VAL).

So, putting that together, you'd have:

<?php
mysql_connect("localhost", "root", "") or
die("Could not connect: " . mysql_error());
mysql_select_db("MYdb");

$result = mysql_query("SELECT max(cont_num) cont_num FROM findates");

$row = mysql_fetch_array($result, MYSQL_NUM));
printf("cont_num: %s", $row[0]);
$newKey = $row[0] + 1;
mysql_query("INSERT finddates (cont_num) values ($newKey)");

}

mysql_free_result($result);

Philip Hallstrom
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Beginner needs help


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
Japhy
Guest
 
Posts: n/a
#4: Jul 17 '05

re: Beginner needs help


Thank you both for your quick and helpful posts. Yes, I did read about
and use max() to get the result I wanted last night....it was somehow
omitted from my post......

Closed Thread


Similar PHP bytes