473,241 Members | 1,597 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,241 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 3347
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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.