473,761 Members | 8,651 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3372
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.andyhsoftwa re.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_r ows; $counter++)
{
update table set columnname=colu mnname+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_r ows; $counter++)
{
update table set columnname=colu mnname+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.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #4
$query = "update table set columnname = " . $variable[$arraypointer];
mysql_query($qu ery);

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($qu ery, $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_r ows; $counter++)
{
update table set columnname=colu mnname+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($qu ery);
$old_value = mysql_result($r esult, 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($qu ery);
if ($result) {
echo 'Success!';
}
else {
echo '<a href="www.php.n et">I better read this carefully</a>';
}

Good Luck!

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

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($qu ery, $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
3847
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 could resolve with a simple function: Example: I have 50records - I want to update a selection of the recods - some,
4
1539
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 (content) with some content and every time I edit it I store all the table contents in an other table (content_bak) when I want to restore the contents How am i going to do that ? I am trying to do :
6
6859
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 : GRANT SELECT , INSERT , UPDATE , DELETE ON sassisc. * TO
0
1814
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 queries(updates are especially slow) are running 20/30 or more times slower than on an inferior(32 bit processor, 1.5GB RAM) machine runnning Mysql 3.23.56 on mandrake 9.0.
57
25523
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
2445
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 externally hosted MySQL database (fasthosts) . I then import-linked
2
2711
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 externally hosted MySQL database (fasthosts) . I then import-linked
2
2694
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 allowed me to ensure that when a record is either added or edited, the value in the field is set to the current date and time. I.E., ab_timestamp is given the current date and time when a record is created, and then it is updated to the date and...
3
8831
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 successful 'sudo make install.' Before every attempt I even do a sudo make distclean to make sure I haven't gotten things mucked up from a prior attempt. OS: Mac OS X 10.4.10 MySQL: v5.0.41 for Mac OSX-i686 DBI: v1.58 (installed fine using CPAN)...
6
38518
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 through this without much trouble. Programming knowledge is not required. Index What is SQL? Why MySQL? Installing MySQL. Using the MySQL command line interface
0
9345
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10115
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9957
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9905
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9775
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7332
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5229
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3456
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.