SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Dec 2003
    Location
    brazil
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ??passing $sql query as a parameter

    Why the following doesn't work?

    $avalues = array();
    $avalues[1] = $datavalue;
    $sql = "UPDATE `table` SET `fieldname` = '$avalues[1]' ";
    doupdate($sql);


    function doupdate($sql);
    global $avalues;
    $result = mysql_query($sql);
    ....
    // $result is ALWAYS false
    // but WAIT!, when I place the $sql =... line
    // inside the function it WORKS!,

    WHY? I'm doing this?

    I intend to have only one function per I/O transaction,
    instead of writing one for each table,
    If I'd had do this, then every time I want to change or test
    a new method for I/O I'd have to change many functions,
    so thew probability to have errors increase.

  2. #2
    SitePoint Enthusiast
    Join Date
    May 2002
    Location
    Uk
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Putting an array into a string won't work unless you:
    PHP Code:
     //Join the strings and the array together using full stops
     
    $sql "UPDATE `table` SET `fieldname` = '" $avalues[1] . "' ";
     
     
    //Enclose the array in curly braces
      
    $sql "UPDATE `table` SET `fieldname` = '{$avalues[1]}' "

  3. #3
    SitePoint Member
    Join Date
    Dec 2003
    Location
    brazil
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    doesn't work either

    same result, if I write the same line into the array it does,
    any way, the idea isn't pass the "value" of the array when
    creating the $sql string,
    but let mysql_query use the value the array has in the moment of execution...

  4. #4
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Vex
    Putting an array into a string won't work unless you:
    [/PHP]
    Want to bet?
    It works, if you go $array[key] (as opposed to $array['key']).


    Anyway, on topic:

    Does:
    $avalues = array();
    $avalues[1] = $datavalue;
    $sql = "UPDATE `table` SET `fieldname` = '$avalues[1]' ";
    mysql_query($sql);

    Work?

    Also, add an "or die(mysql_error());" to it:

    function doupdate($sql);
    global $avalues;
    $result = mysql_query($sql) or die(mysql_error());


    And post back with the error.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •