??passing $sql as a parameter
Moderator: General Moderators
??passing $sql 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.
$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.
- Derfel Cadarn
- Forum Contributor
- Posts: 193
- Joined: Thu Jul 17, 2003 12:02 pm
- Location: Berlin, Germany
Try it like this:
Code: Select all
<?php
$sql = "UPDATE `table` SET `fieldname` = $avalues[1] ";
?>- Derfel Cadarn
- Forum Contributor
- Posts: 193
- Joined: Thu Jul 17, 2003 12:02 pm
- Location: Berlin, Germany
Why are you using:
in the function? You've already sent the variable, you don't need to define $avalues again.
Code: Select all
global $avalues;Code: Select all
function doupdate($sql);please read :
http://www.php.net/manual/en/functions.php
-
Hurklefish
- Forum Newbie
- Posts: 7
- Joined: Sat Dec 20, 2003 4:03 pm
try it like this, perhaps.
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?
?>
$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?
?>
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Re: try it like this, perhaps.
Absolutely, if fieldname is not an integer.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?
?>
Mac