Page 1 of 1

??passing $sql as a parameter

Posted: Sat Dec 20, 2003 1:13 pm
by lisawebs
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.

Posted: Sat Dec 20, 2003 1:29 pm
by Derfel Cadarn
Try it like this:

Code: Select all

<?php
$sql = "UPDATE `table` SET `fieldname` = $avalues[1] "; 
?>

why?

Posted: Sat Dec 20, 2003 2:33 pm
by lisawebs
I'm gonna try it,
but even if it works I don't understand why

Posted: Sat Dec 20, 2003 2:40 pm
by Derfel Cadarn
Well, what I'm trying to say is: I think you don't need those '-thingies around $avalues[1]. But since I've been coding all day, I might be wrong..I feel kinda 8O

====================================
= Yes, I'm trying to get my 101st posting today =
====================================

Posted: Sat Dec 20, 2003 8:52 pm
by DuFF
Why are you using:

Code: Select all

global $avalues;
in the function? You've already sent the variable, you don't need to define $avalues again.

Posted: Sat Dec 20, 2003 10:20 pm
by infolock

Code: Select all

function doupdate($sql);
this function does absolutely nothing.

please read :

http://www.php.net/manual/en/functions.php

try it like this, perhaps.

Posted: Sun Dec 21, 2003 1:51 am
by Hurklefish
rather than this..


$sql = "UPDATE `table` SET `fieldname` = $avalues[1] ";

shouldn't it be like this..


$sql = "UPDATE table SET fieldname = '$avalues[1]' ";

with no single qoutes around the table or fieldnames, but around the value to be updated?




?>

Re: try it like this, perhaps.

Posted: Mon Dec 22, 2003 5:21 am
by twigletmac
Hurklefish wrote:rather than this..

$sql = "UPDATE `table` SET `fieldname` = $avalues[1] ";

shouldn't it be like this..

$sql = "UPDATE table SET fieldname = '$avalues[1]' ";

with no single qoutes around the table or fieldnames, but around the value to be updated?

?>
Absolutely, if fieldname is not an integer.

Mac