473,396 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Beginner needs help

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

Jul 17 '05 #1
3 1306
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);

Jul 17 '05 #2
On 2005-04-01, Japhy <ja************@yahoo.com> wrote:
Hello. I am writing my first web based PHP/MYsql application.
Welcome!
I have used the following code to retrieve the hightest value in a
field :

<?php
mysql_connect("localhost", "root", "") or
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.
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]);
}
The best way to find the maximum value is to use a query like this:

SELECT MAX(cont_num) FROM findates

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;
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
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.


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
Jul 17 '05 #3
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......

Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: BobJohnson | last post by:
Just started learning C++ and I need some help with my homework, shouldn't take long for people around here. I need to create a simple money calculator but I don't know how to make the output...
12
by: Blaze | last post by:
I am doing the first walk through on the Visual Studio .Net walkthrough book to learn a little about programming. I am having issues with the first tutorial not running correctly. It seems that...
3
by: Chung Hang Shum | last post by:
I'm a beginner in ASP.Net. I'd wroten mange asp code before and now I need to transfer them from asp to asp.Net. In classical ASP you can write code like this: <% If strRequest = "ConfirmPasswd"...
18
by: mitchellpal | last post by:
Hi guys, am learning c as a beginner language and am finding it rough especially with pointers and data files. What do you think, am i being too pessimistic or thats how it happens for a beginner?...
0
by: Jarkko Turpeinen | last post by:
I'm a beginner in XML language and I'm trying to create an XML file that could be viewed in Excel as a table that has rows and columns like this: Q1 Q2 Row1 3 5 Row2 4 1 Row3 2 3 But...
2
by: stargate03 | last post by:
Hi there I have a MYSQL database which has the following table CREATE TABLE `#__content` ( `id` int(11) unsigned NOT NULL auto_increment, `title` varchar(100) NOT NULL default '', ...
5
by: mac010904 | last post by:
i'm new to java an have an assignment. i need to write a program that will calculate the sides of a triangle using x and y coordinates.also, it should provide the area, perimeter and lengths of the...
3
by: anke | last post by:
hi all, i am fairly new to css. i know the very basics, can work with scripts and adapt them. i learned webdesign in the late 90ies and have troubles forget about my beloved tables ;-) anyway,...
0
by: gwmarin | last post by:
Hello, i am a complete beginner with visual basic so lets see if i can explain my problem. I've got microsoft excel 2003 which has got Visual Basic 6.5 and i need to create a macro which will click...
22
by: ddg_linux | last post by:
I have been reading about and doing a lot of php code examples from books but now I find myself wanting to do something practical with some of the skills that I have learned. I am a beginner php...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.