Jump to content

[SOLVED] error in mysql syntax, take a look at my syntax is it wrong?


dsaba

Recommended Posts

i'm running this query, I'm unsure from which query the error is calling from but all the queries are structured pretty much the same

<?php
$updatequery = mysql_query("UPDATE `dictionary` SET `english`='$sourceline', `he_flagged`='Yes', `es_flagged`='Yes', `pt_flagged`='Yes', `pl_flagged`='Yes' LIMIT $i, 1") or die(mysql_error());

$updatequery = mysql_query("UPDATE `dictionary` SET `english`='$sourceline', `he_flagged`='Yes', `es_flagged`='Yes', `pt_flagged`='Yes', `pl_flagged`='Yes' LIMIT $i, 1") or die(mysql_error());
?>

 

$i will be looping from 0 to 299

 

this is the error:

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 ' 1' at line 1

 

How do I fix it?

 

-thanks

Link to comment
Share on other sites

On update the LIMIT clause places a limit on the number of rows that can be updated.

 

You can use LIMIT row_count to restrict the scope of the UPDATE. A LIMIT clause is a rows-matched restriction. The statement stops as soon as it has found row_count rows that satisfy the WHERE clause, whether or not they actually were changed.

 

 

On SELECT

The LIMIT clause can be used to constrain the number of rows returned by the SELECT  statement. LIMIT takes one or two numeric arguments, which must both be non-negative integer constants (except when using prepared statements).

 

With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):

 

EDIT:

to sum up, Update can only  have 1 number

Link to comment
Share on other sites

here's my full code with context of my query:

<?php

$i = -1;
while ($queryrow = mysql_fetch_array($query))    {
$i++;
$oldenglish = $queryrow['english'];
$sourceline = trim($fileArray[$i]);

if ($oldenglish != $sourceline) {
$updatequery = mysql_query("UPDATE `dictionary` SET `english`='$sourceline', `he_flagged`='Yes', `es_flagged`='Yes', `pt_flagged`='Yes', `pl_flagged`='Yes' LIMIT $i, 1") or die(mysql_error());

?>

 

I was trying to update only a certain row which i specify, now I see that this cannot be done

 

But what if I added a WHERE clause like this:

$updatequery = mysql_query("UPDATE `dictionary` SET `english`='$sourceline', `he_flagged`='Yes', `es_flagged`='Yes', `pt_flagged`='Yes', `pl_flagged`='Yes' WHERE fieldname='value' LIMIT $i, 1") or die(mysql_error());

 

then can I do that? I really need to update by certain rows, can anyone tell me how to do this?

 

-thank you

Link to comment
Share on other sites

i'm fully aware of the unique id advantages, but i'm in a situation where the entries in my database must correlate to the line count in a .txt file

 

i was doing it with ids, but sometimes i delete rows in my database and therefore the ids are off, so i want to do it by row#

 

not being able to update a record by a particular row seems very odd, for such capabilities in mysql, you can use LIMIT to select a single row with SELECT , why not with UPDATE

 

that just seems odd, there must be a way...  any ideas????

Link to comment
Share on other sites

what about

<?php
$updatequery = mysql_query("UPDATE `dictionary` SET `english`='$sourceline', `he_flagged`='Yes', `es_flagged`='Yes', `pt_flagged`='Yes', `pl_flagged`='Yes' WHERE ID = (SELECT ID FROM `dictionary` WHERE fieldname='value' LIMIT $i, 1)") or die(mysql_error());

?>

 

 

May wanna backup the database first, thats 101% untested

Link to comment
Share on other sites

I ran this in sql in phpmyadmin:

UPDATE `dictionary` SET `english`='hello', `he_flagged`='Yes', `es_flagged`='Yes', `pt_flagged`='Yes', `pl_flagged`='Yes' WHERE ID = (SELECT ID FROM `dictionary` LIMIT 6, 1)

 

it said this:

Error

SQL query:

 

UPDATE `dictionary` SET `english` = 'hello',

`he_flagged` = 'Yes',

`es_flagged` = 'Yes',

`pt_flagged` = 'Yes',

`pl_flagged` = 'Yes' WHERE ID = ( SELECT ID

FROM `dictionary`

LIMIT 6 , 1 )

MySQL said: 

 

#1093 - You can't specify target table 'dictionary' for update in FROM clause

 

what does that mean?

Link to comment
Share on other sites

<?php
//update and/or add new words to english from source
$query = mysql_query("SELECT * FROM dictionary") or die(mysql_error());
$totalentries = mysql_num_rows($query);
$something = $totalentries - 1;
echo 'There are <b>'.$totalentries.'</b> entries in the database<br>';

$fileArray = file('dictionary/source.txt') or die("cannot make array out of file lines");
$totalsource = count($fileArray);
$difference = $totalsource - $totalentries;
echo 'There are <b>'.$totalsource.'</b> entries in the source<br>';
echo 'There are <b>'.$difference.'</b> NEW entries in the source<br><br>';
echo 'This means that rows <b>0-'.$something.'</b> are going to be UPDATED in the database<br>';
echo 'and <b>'.$difference.'</b> entries are going to be INSERTED in the database<br><br>';

if ($totalsource > $totalentries)					{
//update and add entries
//update first
$i = -1;
while ($queryrow = mysql_fetch_array($query))	{
$i++;
$rownum = $i + 1;
$oldenglish = $queryrow['english'];
$sourceline = trim($fileArray[$i]);

if ($oldenglish != $sourceline) {
$updatequery = mysql_query("UPDATE `dictionary` SET `english`='$sourceline', `he_flagged`='Yes', `es_flagged`='Yes', `pt_flagged`='Yes', `pl_flagged`='Yes' LIMIT $i, 1") or die(mysql_error());
if ($updatequery == "1") {
echo '<b>'.$oldenglish.'</b> <em>replaced with</em> <b>'.$sourceline.'</b><br>';
} else {
echo 'failure nothing replaced<br>';
}

}
				    //end of while statement
											}
?>

 

the brackets look better here:

http://phpfi.com/229752

Link to comment
Share on other sites

thanks a lot for the help, but I was originally looking for a simple solution, turns out its getting more and more complicated

 

i have thought of a simple solution though if i want to correlate lines from a .txt file and my table

 

i will have to use ids, but instead of deleting the rows if i want to remove an entry in the table, i will just have a fieldname named 'status' which will have active or inactive

 

i will just mark it inactive, and not query it, so that way the ids will still correlate

 

I appreciate you trying

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.