Jump to content

Problem parsing update statement


bugzy

Recommended Posts

When I tried to parse it, the code suddenly is gibing me an error

 

<?php

$query3 = "Update item_category ";

		for($i=0;$i <= $row;$i++)
		{
			$get_id = mysql_result($result2, $i, 'Item');

			if(mysql_result($result2,$i,'Total') == 1)
			{				
				$query3 .= "SET category_id=1 where item_id = {$get_id},";

			}

		}



		$result3 = mysql_query(trim($query3,','),$connection) or die (mysql_error());

?>

 

 

I believe that I'm parsing the update statement the wrong way..

 

Anyone?

Link to comment
Share on other sites

The error might be useful?

 

 

From this code

 

<?php for($i=0;$i <= $row;$i++)
		{
			$get_id = mysql_result($result2, $i, 'Item');

			if(mysql_result($result2,$i,'Total') == 1)
			{				
				$query3 = "Update item_category set category_id=1 where item_id = {$get_id}";
				$result3 = mysql_query($query3,$connection) or die (mysql_error());
			}

		} ?>

 

 

 

to this

 

 

 

<?php

$query3 = "Update item_category ";

for($i=0;$i <= $row;$i++)

{

$get_id = mysql_result($result2, $i, 'Item');

if(mysql_result($result2,$i,'Total') == 1)


{

$query3 .= "SET category_id=1 where item_id = {$get_id},";

}


}

$result3 = mysql_query(trim($query3,','),$connection) or die (mysql_error());

?>


 

 

 

I have change only the Update portion inside the for loop because I don't want to put a sql statement inside a loop, and I got an error already...

 

Maybe I'm parsing it the wrong way or I place it on top of the loop which interfere it?

 

 

Anyone?

Link to comment
Share on other sites

The error might be useful?

 

By that I mean the actual error message PHP gives you.

 

 

Warning: mysql_result() [function.mysql-result]: Unable to jump to row 2 on MySQL result index 17 in C:\wamp\www\runa\admin\delete_category.php on line 91

 

Warning: mysql_result() [function.mysql-result]: Unable to jump to row 2 on MySQL result index 17 in C:\wamp\www\runa\admin\delete_category.php on line 93

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 'SET category_id=1 where item_id = 160' at line 1

 

 

AGain, it only happened when I change the code from my above post.. Maybe I'm putting the first statement on the wrong way?

Link to comment
Share on other sites

 

for($i=0;$i <= $row;$i++)

{

$get_id[$i] = mysql_result($result2, $i, 'Item');

if(mysql_result($result2,$i,'Total') == 1)

{

 

$query3 = mysql_query("Update item_category SET category_id='1' where item_id = '$get_id[$i]'");

 

}// if

}// for

Link to comment
Share on other sites

 

for($i=0;$i <= $row;$i++)

{

$get_id[$i] = mysql_result($result2, $i, 'Item');

if(mysql_result($result2,$i,'Total') == 1)

{

 

$query3 = mysql_query("Update item_category SET category_id='1' where item_id = '$get_id[$i]'");

 

}// if

}// for

 

 

Again, as much as possible I don't want to put any sql query/statement inside a loop...

Link to comment
Share on other sites

Hello guys base on my research seemed like I can't update multiple rows with different condition in one mysql query..

 

 

Any idea guys how can I put this in just one single query?

 

Update item_category

 

SET category_id=1 where item_id = 166

SET category_id=1 where item_id = 167

 

 

I mean is there a work around?

Link to comment
Share on other sites

Update item_category SET category_id=1 where item_id IN(166, 167)

 

Thanks  jesirose. It's working though I'm having problem with ",";

 

Here's the new code

 

<?php

		$query3 = "Update item_category SET category_id=1 where item_id IN (";

		for($i=0;$i <= $row;$i++)
		{
			$get_id = mysql_result($result2, $i, 'Item');

			if(mysql_result($result2,$i,'Total') == 1)
			{				
				$query3 .= "$get_id,";

			}

		}

		$query3 .= ")";

		$result3 = mysql_query(trim($query3,','),$connection);

?>

 

 

It's giving me

 

Update item_category SET category_id=1 where item_id IN (177,178,179,)

 

 

How would I able to remove the comma on the last item_id?

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.