In article <[EMAIL PROTECTED]>, Phil Schwarzmann wrote:
> $query = "UPDATE table SET (var1, var2, var3) VALUES ($var1, $var2,
> $var3) WHERE username='$username'";
>  
>  
> Im wondering cause I have an INSERT query....
>  
> $query = "INSERT INTO table (var1, var2, var3) VALUES ($var1, $var2,
> $var)";
>  
> ...but only there are like 150 different variables and it will take me
> forever to write a query like....

Unfortuantely, UPDATE syntax is in fact completely different. This is
usually a good thing - imagine counting commas with those 150 columns to
see if you put #93 in the 93rd or 94th spot!

It sounds like you need to normalize your data more. I can't imagine
anything which would require that many columns in one table and it's
usually better to break things into multiple tables where possible. If
you can, see if you can't restructure the database considerably - you'll
save yourself a great deal of work later.

Assuming you can't avoid setting that many variables at once, you can
use a PHP script like this to generate some statements programatically:

<?
        header("Content-Type: text/plain");

        if (empty($_REQUEST['Table'])) {
                die("Please provide a URL parameter Table");
        } else {
                $Table = mysql_escape_string($_REQUEST['Table']);
        }

        print "-- Generated by " . basename(__FILE__) . " from $Table table on " . 
date("Y-m-d H:i") . "\n";
        
        mysql_connect("localhost", "USER", "PASSWORD");

        $fields = mysql_list_fields("DATABASE", $Table) or die(mysql_error());
        $columns = mysql_num_fields($fields) or die(mysql_error());

        print "UPDATE $Table SET\n";
        
        for ($i = 0; $i < $columns; $i++) {
                $name = mysql_field_name($fields, $i);
                $type = mysql_field_type($fields, $i);
                $flags = mysql_field_flags($fields, $i);

                print "\t$name = ";
                switch ($type) {
                        case "int":
                                print "\$$name";
                                break;

                        case "string":
                        case "blob":
                                print "'\$$name'";
                                break;

                        case "datetime":
                                print "'\$$name'";
                                break;

                        default:
                                print "unknown type $type";

                }

                print " /* type: $type flags: $flags */";

                if ($columns - $i > 1) print ",";
                print "\n";
        }
        
        print "WHERE ...\n";
?>

(As a side note, MySQL supports the UPDATE style syntax for INSERT and
has a REPLACE command which will UPDATE if the record exists and INSERT
if it doesn't - this can help you maintain only one query)

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to