whats wrong with this????????

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
hward
Forum Contributor
Posts: 107
Joined: Mon Apr 19, 2004 6:19 pm

whats wrong with this????????

Post by hward »

what am i missing here?

Code: Select all

<?php
$sql = " 
  UPDATE $table_name 
  SET print = 'no'   
  WHERE id = 'id_1'
  ";
?>
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Aren't ID's usually numeric?

Anyways, you never execute the query.

Code: Select all

<?php
$conn = mysql_connect("user","pass","localhost");
mysql_select_db("mydatabase");
$table_name = "mytable";
$sql = "UPDATE $table_name SET print = 'no' WHERE id = 'id_1'";
mysql_query($sql);
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

what am i missing here?
you tell us..
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

with a query like that, better off to throw in a:

or die(mysql_error());

in there =]
User avatar
Lord Sauron
Forum Commoner
Posts: 85
Joined: Tue Apr 20, 2004 5:53 am
Location: Tilburg, NL

Post by Lord Sauron »

It isn't that hard :wink:

Just leave the $-sign out, when your using a table name. And use a $-sign when you are using a php-variable (I guess id_1 is a php variable?)

$sql = "UPDATE table_name SET print = 'no' WHERE id = '$id_1'";
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

ahhh! evil post digger! *puts fingers crossed* be gone evil doer! :lol:
anywho, he may be setting the table dynamically or above the query, meaning the $ should be there.
User avatar
Vash the Stampede
Forum Newbie
Posts: 7
Joined: Tue Mar 30, 2004 6:15 pm

Post by Vash the Stampede »

LiLpunkSkateR wrote:anywho, he may be setting the table dynamically or above the query, meaning the $ should be there.
If he is setting it dynamically shouldn't he do it like this (assuming he has already done the connection the the database earlier):

Code: Select all

<?php
$table_name = "mytable";
$sql = "UPDATE ". $table_name ." SET print = 'no' WHERE id = 'id_1'";
?>
I always have had problems when putting a var in with out the ". $var ." in sql statements.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Code: Select all

$sql = "SELECT * FROM $table"; //valid
$sql = "SELECT * FROM " . $table; //better
Both work.

Code: Select all

$sql = 'SELECT * FROM $table'; //NOT valid
does not work however.
Post Reply