473,408 Members | 1,749 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,408 software developers and data experts.

Mysql update? help required.

Hello all.
I'm totally stumped. Please de-stump me!

I've read my data in from the database, changed it according to what i
want to do and now I want to write it back.

But it seems I can only use the update statement will only accept
explicit fieldnames and value. The fieldnames I can live with, I only
have 5 columns in the database, but why can't I pass a $variable to
this statement?

I realise its a MySQL statement and not PHP. Either I'm missing
something pretty fundamental or this whole PHP/MySQL thing is useless.
What is the point if you cannot write back $variables?

I'm just a simple soul. Answers in words of less than 2 syllables
please. :)
Jul 17 '05 #1
6 3352
On 27 Mar 2005 05:21:00 -0800, pa**@moontech-racing.com (Paul Eden) wrote:
I've read my data in from the database, changed it according to what i
want to do and now I want to write it back.

But it seems I can only use the update statement will only accept
explicit fieldnames and value. The fieldnames I can live with, I only
have 5 columns in the database, but why can't I pass a $variable to
this statement?

I realise its a MySQL statement and not PHP. Either I'm missing
something pretty fundamental or this whole PHP/MySQL thing is useless.
What is the point if you cannot write back $variables?


You can. What have you tried?

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #2
Andy Hassall <an**@andyh.co.uk> wrote in message news:<0a********************************@4ax.com>. ..
On 27 Mar 2005 05:21:00 -0800, pa**@moontech-racing.com (Paul Eden) wrote:
I've read my data in from the database, changed it according to what i
want to do and now I want to write it back.

But it seems I can only use the update statement will only accept
explicit fieldnames and value. The fieldnames I can live with, I only
have 5 columns in the database, but why can't I pass a $variable to
this statement?

I realise its a MySQL statement and not PHP. Either I'm missing
something pretty fundamental or this whole PHP/MySQL thing is useless.
What is the point if you cannot write back $variables?


You can. What have you tried?


I asked Google and it came up with a page (which I now can't find)
that gave a function to do it. It claimed to be nicely commented and
documented by frankly I couldn't follow it and in any case, my PHP
knowledge doesn't extend to functions just yet.
Obviousy, I wrote
update table set columnname = $variable[$arraypointer];
and it threw it out.
I've had an idea though; please feel free to shoot it down;
The data I want to write back to the table is numerical and only ever
increases. Could I;
$counter = $newvalue-$currentvalue;
for ($counter=0; $counter<$num_rows; $counter++)
{
update table set columnname=columnname+1
}
in other words, as the value i want to write back only ever increases,
I just work out the difference between new and current, and then using
a loop, increment the value that many times?
Jul 17 '05 #3
On 27 Mar 2005 10:55:04 -0800, pa**@moontech-racing.com (Paul Eden) wrote:
Andy Hassall <an**@andyh.co.uk> wrote in message news:<0a********************************@4ax.com>. ..
On 27 Mar 2005 05:21:00 -0800, pa**@moontech-racing.com (Paul Eden) wrote:
I realise its a MySQL statement and not PHP. Either I'm missing
something pretty fundamental or this whole PHP/MySQL thing is useless.
What is the point if you cannot write back $variables?
You can. What have you tried?


I asked Google and it came up with a page (which I now can't find)
that gave a function to do it. It claimed to be nicely commented and
documented by frankly I couldn't follow it and in any case, my PHP
knowledge doesn't extend to functions just yet.


Well, you'll have to call a function. mysql_query(), at least.

http://uk.php.net/mysql_query

The manual link above has several examples.
Obviousy, I wrote
update table set columnname = $variable[$arraypointer];
and it threw it out.
With what error? Always post the errors you receive, along with the smallest
code you can reduce down to demonstrating your problem.
I've had an idea though; please feel free to shoot it down;
The data I want to write back to the table is numerical and only ever
increases. Could I;
$counter = $newvalue-$currentvalue;
for ($counter=0; $counter<$num_rows; $counter++)
{
update table set columnname=columnname+1
You can't just put SQL in PHP and expect it to work, you have to call
mysql_query(). Also, this has to be the most insane way ever to set a value in
a database :-)
}
in other words, as the value i want to write back only ever increases,
I just work out the difference between new and current, and then using
a loop, increment the value that many times?


--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #4
$query = "update table set columnname = " . $variable[$arraypointer];
mysql_query($query);

You have to write your SQL in a string variable, then pass that to
mysql_query(). Of course, you'll need to open a database connection as
well (if you haven't already). You've probably figured that out since
you managed to get data out of the database. In that case, use:

mysql_query($query, $dbconn);

where $dbconn is your link identifier returned by mysql_connect().

Jul 17 '05 #5
On 27 Mar 2005 10:55:04 -0800, in comp.lang.php
pa**@moontech-racing.com (Paul Eden) wrote:

I asked Google and it came up with a page (which I now can't find)
that gave a function to do it. It claimed to be nicely commented and
documented by frankly I couldn't follow it and in any case, my PHP
knowledge doesn't extend to functions just yet.
Obviousy, I wrote
update table set columnname = $variable[$arraypointer];
and it threw it out.
I've had an idea though; please feel free to shoot it down;
The data I want to write back to the table is numerical and only ever
increases. Could I;
$counter = $newvalue-$currentvalue;
for ($counter=0; $counter<$num_rows; $counter++)
{
update table set columnname=columnname+1
}
in other words, as the value i want to write back only ever increases,
I just work out the difference between new and current, and then using
a loop, increment the value that many times?


Before you increment the value, you have to query the database to see
what it was previously:

$query = "SELECT value FROM tablename WHERE column = '$old_value'";
$result = mysql_query($query);
$old_value = mysql_result($result, 0, 'column');

Now that you know the old value, you can increment it by one without
any loop:

$new_value = $old_value + 1;

Now you can do your update whith the new value:

$query = "UPDATE tablename SET column = '$new_value' WHERE column =
'$old_value'";
$result = mysql_query($query);
if ($result) {
echo 'Success!';
}
else {
echo '<a href="www.php.net">I better read this carefully</a>';
}

Good Luck!

--sketch
Jul 17 '05 #6
"ZeldorBlat" <ze********@gmail.com> wrote in message news:<11**********************@o13g2000cwo.googleg roups.com>...
$query = "update table set columnname = " . $variable[$arraypointer];
mysql_query($query);

You have to write your SQL in a string variable, then pass that to
mysql_query(). Of course, you'll need to open a database connection as
well (if you haven't already). You've probably figured that out since
you managed to get data out of the database. In that case, use:

mysql_query($query, $dbconn);

where $dbconn is your link identifier returned by mysql_connect().


Thanks for all your help. I did understand much of it. :)

I decided to use flat data files in the end as I could work out how to
use them on my own and the complexity of the task didn't really
warrant using a database.

Once again. Many thanks.
Jul 17 '05 #7

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

Similar topics

2
by: Reply via newsgroup | last post by:
Folks, When performing an update in mysql (using PHP), can I find out how many records were matched? mysql_affected_rows() won't work... and I have the following problem that I thought I...
4
by: Angelos | last post by:
Hello, First of all sorry if this is not the correct newsgroup for this question, but I am using PHP with MYSQL and someone here could have an answer and the experience. I have the a table...
6
by: Xerxes | last post by:
When I use the following GRANT statement GRANT SELECT , INSERT , UPDATE , DELETE ON sassisc.* TO sassisc@localhost IDENTIFIED BY 'dodge3' it came back with an error message: SQL-query : ...
0
by: Scott | last post by:
Hi, I'm having a problem with a new machine running Mysql version 4.0.18 on the AMD64 version of Mandrake 10.0. The new machine has got 64bit AMD processor and 2GB of RAM. Nearly all...
57
by: Bing Wu | last post by:
Hi all, I am running a database containing large datasets: frames: 20 thousand rows, coordinates: 170 million row. The database has been implemented with: IBM DB2 v8.1
1
by: gordon.dtr | last post by:
Hi, Has anyone had this problem ? I am using MySQL ODBC 3.51 Driver, with MS Access 2003 and MySQL 4.1.11 standard log. I created my tables in MS Access, then exported them via ODBC to an...
2
by: David | last post by:
Hi, Has anyone had this problem ? I am using MySQL ODBC 3.51 Driver, with MS Access 2003 and MySQL 4.1.11 standard log. I created my tables in MS Access, then exported them via ODBC to an...
2
by: Ted | last post by:
1) In several tables, in my MySQL version, I created columns using something like the following: `ab_timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, This...
3
by: menzies | last post by:
Hi, I"m new to this forum, but I have been trying all day to install DBD::mysql onto my Intel MacBook. I've read lots of forums pages and none have gotten me to a successful 'make test' or a...
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.