Simple SQL query problem

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

Simple SQL query problem

Post by hward »

What am I missing here?? I think i am having a major brain fart!

Code: Select all

$sql = "
  UPDATE $table_name 
  SET sold = 'yes'   
  WHERE id = \"$id\""; 
$result = @mysql_query($sql,$connection)
	or die("Couldn't execute query");
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$table_name or $connection would make it have problems.... it'd help to know some more about the code..... try using mysql_error() with the die() instead of just a string.
hward
Forum Contributor
Posts: 107
Joined: Mon Apr 19, 2004 6:19 pm

Post by hward »

Code: Select all

$db_name = "db";
$table_name = "item";
$id = "20";
$connection = @mysql_connect("localhost", "username", "password") 
	or die("Couldn't connect.");
 
$sql = "
  UPDATE $table_name 
  SET sold = 'yes'   
  WHERE id = \"$id\""; 
$result = @mysql_query($sql,$connection)
	or die("Couldn't execute query");

print "udated";
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Select the database with mysql_select_db() and try again :)
hward
Forum Contributor
Posts: 107
Joined: Mon Apr 19, 2004 6:19 pm

Post by hward »

same results


Couldn't execute query
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

guess you didn't see the part where I said to use mysql_error() ... :roll:
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

add/change the die() to

Code: Select all

die(mysql_error());
as feyd suggested.
hward
Forum Contributor
Posts: 107
Joined: Mon Apr 19, 2004 6:19 pm

Post by hward »

Works now thanks

Code: Select all

$db_name = "db"; 
$table_name = "item"; 
$id = "20"; 
$connection = @mysql_connect("localhost", "username", "password") 
    or die("Couldn't connect."); 
$db = @mysql_select_db($db_name, $connection) or die("couldn't select database");
$sql = " 
  UPDATE $table_name 
  SET sold = \"yes\"    
  WHERE id = \"$id\"
  "; 
$result = @mysql_query($sql,$connection) 
    or die("Couldn't execute query"); 

print "udated";
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

np - delayed post
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

You don't need to assign variables to some of those functions, as they will or do not return anything, or they do not reutrn anything of use.

Code: Select all

<?php

/* this function does not return any value, no variable needed */
@mysql_select_db($db_name, $connection) or die("couldn't select database");

/* this will not return anything of use in this context, you should only use a variable if you are expecting a result set to be returned, i.e. you are using SELECT instead of INSERT, UPDATE, DELETE etc. */
$sql	=	"
			UPDATE $table_name
			SET sold = 'yes'   
			WHERE id = '$id'
			";
@mysql_query($sql,$connection) or die("Couldn't execute query");

?>
Post Reply