Page 1 of 1

Simple SQL query problem

Posted: Wed Sep 21, 2005 10:15 am
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");

Posted: Wed Sep 21, 2005 10:16 am
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.

Posted: Wed Sep 21, 2005 10:20 am
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";

Posted: Wed Sep 21, 2005 10:40 am
by Jenk
Select the database with mysql_select_db() and try again :)

Posted: Wed Sep 21, 2005 10:46 am
by hward
same results


Couldn't execute query

Posted: Wed Sep 21, 2005 10:54 am
by feyd
guess you didn't see the part where I said to use mysql_error() ... :roll:

Posted: Wed Sep 21, 2005 10:55 am
by Jenk
add/change the die() to

Code: Select all

die(mysql_error());
as feyd suggested.

Posted: Wed Sep 21, 2005 11:02 am
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";

Posted: Wed Sep 21, 2005 11:04 am
by Maugrim_The_Reaper
np - delayed post

Posted: Wed Sep 21, 2005 11:08 am
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");

?>