You have an error in your SQL syntax?

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

mayanktalwar1988
Forum Contributor
Posts: 133
Joined: Wed Jul 08, 2009 2:44 am

You have an error in your SQL syntax?

Post by mayanktalwar1988 »

Code: Select all

<?php
$filename = "keywords.txt";
$filehandle = fopen($filename, 'w') or die("can't open file");
include("config.php");
$query="select * from downloads";
$result=mysql_query($query) or die(mysql_error());
$query1="update downloads set keyword=1 where release=1";
mysql_query($query1) or die(mysql_error());
while($word=mysql_fetch_array($result,MYSQL_BOTH))
{
if($word['release']==1)
{


$string=$string." ".$word['title'];
}
}
fwrite($filehandle, $string);
fclose($filehandle);

?>
the below line

Code: Select all

$query1="update downloads set keyword=1 where release=1";
is giving this error

Code: Select all

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'release=1' at line 1
...whats wrong with update query?
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: You have an error in your SQL syntax?

Post by andyhoneycutt »

release is a reserved word. wrap tics around it:

Code: Select all

update downloads set keyword=1 where `release`=1
mayanktalwar1988
Forum Contributor
Posts: 133
Joined: Wed Jul 08, 2009 2:44 am

Re: You have an error in your SQL syntax?

Post by mayanktalwar1988 »

k now error gone away buts it not setting the keyword value to 1 where release=1
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: You have an error in your SQL syntax?

Post by andyhoneycutt »

run this, and let me know the result:

Code: Select all

SELECT COUNT(*) FROM downloads WHERE `release`=1;
mayanktalwar1988
Forum Contributor
Posts: 133
Joined: Wed Jul 08, 2009 2:44 am

Re: You have an error in your SQL syntax?

Post by mayanktalwar1988 »

your query gives 30
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: You have an error in your SQL syntax?

Post by andyhoneycutt »

is keyword an enum, or a char?
mayanktalwar1988
Forum Contributor
Posts: 133
Joined: Wed Jul 08, 2009 2:44 am

Re: You have an error in your SQL syntax?

Post by mayanktalwar1988 »

its an int
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: You have an error in your SQL syntax?

Post by andyhoneycutt »

ok, do this then:

Code: Select all

SELECT COUNT(*) FROM downloads WHERE keyword = 1;
mayanktalwar1988
Forum Contributor
Posts: 133
Joined: Wed Jul 08, 2009 2:44 am

Re: You have an error in your SQL syntax?

Post by mayanktalwar1988 »

right now value for the keyword column is null so this will give a zero..i checked it its a zero
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: You have an error in your SQL syntax?

Post by andyhoneycutt »

Could you give me a "SHOW CREATE TABLE downloads"?
mayanktalwar1988
Forum Contributor
Posts: 133
Joined: Wed Jul 08, 2009 2:44 am

Re: You have an error in your SQL syntax?

Post by mayanktalwar1988 »

i am not getting this there are 30 rows for which release has the value =1 but when i run this query

Code: Select all

update downloads set keyword=1 where 'release'=1
it says zero rows effetecd even when the value of rows in keyword column is zero
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: You have an error in your SQL syntax?

Post by andyhoneycutt »

Oh, use backticks as I showed above: ` not '
' is for values.
mayanktalwar1988
Forum Contributor
Posts: 133
Joined: Wed Jul 08, 2009 2:44 am

Re: You have an error in your SQL syntax?

Post by mayanktalwar1988 »

here it goes

Code: Select all

CREATE TABLE IF NOT EXISTS `downloads` (
  `title` text,
  `downloadlink` text,
  `pagelink` text,
  `category` text,
  `release` int(1) NOT NULL,
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `keyword` int(1) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=376 ;
mayanktalwar1988
Forum Contributor
Posts: 133
Joined: Wed Jul 08, 2009 2:44 am

Re: You have an error in your SQL syntax?

Post by mayanktalwar1988 »

k i mistaken back ticks for single inverted commas sorry i wasted your time..thanks
mayanktalwar1988
Forum Contributor
Posts: 133
Joined: Wed Jul 08, 2009 2:44 am

Re: You have an error in your SQL syntax?

Post by mayanktalwar1988 »

ya its worked backticks worked sorry again
Post Reply