??passing $sql as a parameter

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lisawebs
Forum Commoner
Posts: 44
Joined: Wed Nov 19, 2003 6:21 pm

??passing $sql as a parameter

Post 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.
User avatar
Derfel Cadarn
Forum Contributor
Posts: 193
Joined: Thu Jul 17, 2003 12:02 pm
Location: Berlin, Germany

Post by Derfel Cadarn »

Try it like this:

Code: Select all

<?php
$sql = "UPDATE `table` SET `fieldname` = $avalues[1] "; 
?>
lisawebs
Forum Commoner
Posts: 44
Joined: Wed Nov 19, 2003 6:21 pm

why?

Post by lisawebs »

I'm gonna try it,
but even if it works I don't understand why
User avatar
Derfel Cadarn
Forum Contributor
Posts: 193
Joined: Thu Jul 17, 2003 12:02 pm
Location: Berlin, Germany

Post 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 =
====================================
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

Code: Select all

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

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.

Post 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?




?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Re: try it like this, perhaps.

Post 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
Post Reply