Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mysql Update Threads 2

Status
Not open for further replies.

Kennya1

Programmer
Joined
Apr 14, 2002
Messages
15
Location
US
I have a Table Serial with 3 fields ID, FName, Lname

whe I type in with php

$user = "user";
$pass = "password";
$db = "Database";
$link = @mysql_connect( "localhost", $user, $pass );
if ( ! $link ) {
die( "Couldn't connect to MySQL: ".mysql_error() );
}
$sql = 'UPDATE `Serial` SET `FName` = \'Dick\' WHERE `ID` = \'3\'';
$result = mysql_query($sql ,$link );

it works but if I use a variable IT DOES NOT

$user = "user";
$pass = "password";
$db = "Database";
$link = @mysql_connect( "localhost", $user, $pass );
if ( ! $link ) {
die( "Couldn't connect to MySQL: ".mysql_error() );
}
$idnum = 3;
$sql = 'UPDATE `Serial` SET `FName` = \'Dick\' WHERE `ID` = $idnum';
$result = mysql_query($sql ,$link );

I get the following error

Unknown column '$idnum' in 'where clause'

HOW COME???
 
I'm not a PHP expert, but is it like Perl in that strings within single-quotes are not interpolated, but strings within double-quotes are? In other words, would you need to use:[tt]
$sql = "UPDATE `Serial` SET `FName` = \'Dick\' WHERE `ID` = $idnum";
[/tt]


-----
ALTER world DROP injustice, ADD peace;
 
In PHP I'd do it like this :
Code:
$sql = "UPDATE `Serial` SET `FName` = 'Dick' WHERE `ID` = '$idnum'";

* please note , this may not be the best / right / correct way of doing it, but it works for me.


______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top