Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Update syntax

Posted by bschultz (bschultz), 11 February 2004
I'm working on an "edit" or "update" script.  I've got it working, but now want to "simplify" the script.

The update syntax I'm using is this:

Code:
$sqlquery = ("UPDATE $db_name SET T1='$T1', T2='$T2', T3='$T3', T4='$T4', T5='$T5'");


This works just fine.  

I wrote a loop (since I have over 100 field names to update) to step through the fields.  That loop goes like this:

Code:
<?php

for ($i = 1; $i < 142; $i++) {

$set = 'T' . $i;

print "$set="; print "'$$set', ";
}
echo '");'
?>



That also works just fine.  The problem that I'm running into is how to get the loop into the UPDATE.  I've read up on the mysql site, and can't find any tutorials to include the loop into the update sqlquery.  

I've tried to make the loop as a variable:

"$loop = "loop script here"

and then just call the variable, but I have so many quotes in there, I can't seem to get it to work.  Can someone give me a push in the right direction, or a link to what the correct way to do this is?

Thanks.

Brian


Posted by admin (Graham Ellis), 11 February 2004
Rather than print the query, build it up in a string with a .= operator ....  thus

Code:
$query = "UPDATE $db_name SET ";
for ($i=1; $i<142; $i++) {
    $query .= "T$i = '$tab[$i]' ";
    if ($i < 141) $query .= ",";
}


Note the care I've taken to avoid a trailing comma!  I've also assumed your incoming values are in an array.

Try it first by printing out the query ...

Posted by bschultz (bschultz), 11 February 2004
2 things...

1-where did the $tab come from?

2- this isn't in an array, but I can certainly put it in one.  I'll work on it some tonight.  Better get back to the "real" job now.  Thanks.

Brian

Posted by admin (Graham Ellis), 11 February 2004
You put your data into $tab before you get to this code - it's just a place that my loop gets its data from.  

Posted by bschultz (bschultz), 11 February 2004
makes sense...just wanted to make sure that wasn't some function I'm not aware of.

Posted by bschultz (bschultz), 12 February 2004
This is where I am now...

Code:
$j1 = join (", T",range(1,141));
$tab = ("$j1");

$query = "UPDATE $gamedate SET ";
for ($i=1; $i<142; $i++) {
    $query .= "T$i = '$tab[$i]' ";
    if ($i < 142) $query .= ",";
}


And then I printed the query...here's what it said:

Quote:
UPDATE bhockey_2_10 SET T1 = ',' ,T2 = ' ' ,T3 = 'T' ,T4 = '2' ,T5 = ',' ,T6 = ' ' ,T7 = 'T' ,T8 = '3' ,T9 = ',' ,T10 = ' ' ,T11 = 'T' ,T12 = '4' ,T13 = ',' ,T14 = ' ' ,T15 = 'T' ,T16 = '5' ,T17 = ',' ,T18 = ' ' ,T19 = 'T' ,T20 = '6' ,T21 = ',' ,T22 = ' ' ,T23 = 'T' ,T24 = '7' ,T25 = ',' ,


You'll notice that the values are very random...some commas, some integers...when it should print out the variable name.

Where are those values coming from?

Posted by admin (Graham Ellis), 12 February 2004
They're coming from $tab.

You have set up $tab as a string containing
    1, 2, 3, 4 etc
and not a a normal array.   You're then stepping through that string character by character, putiing each character in turn into your query.   I don't think it's what you intended  

Try instead
    $tab = range (1,141);
for testing



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho