Jump to content

My code runs but does not work ?


Topshed

Recommended Posts

mySQL ver: 5.1.30

PHP  ver: 5.2.8

 

I have a piece of code that completes but does nothing.

 

dbase: buses

Table : st

 

Fields used in the code

Seats: CHR (8)

Body:  CHR (22)

UPDATE st SET seats = REPLACE(seats, 'H29/20R ', 'H25/27RO') WHERE body LIKE 'Thomas Tilling'

 

Records 1140

Records to be changed 300

 

As I hope you can see I am trying to replace  'H29/20R ' in seats  WHEN  'Thomas Tilling' occurs in the body field

 

You help would be  much appreciated

Link to comment
https://forums.phpfreaks.com/topic/259252-my-code-runs-but-does-not-work/
Share on other sites

don't use REPLACE in an UPDATE query, it's only for SELECT.  Just include the value that you want the field to contain when you have run the UPDATE:

UPDATE st SET seats = 'H25/27RO' WHERE body LIKE 'Thomas Tilling'

 

As an afterthought, if you know the exact contnent of the field that you are looking up in the WHERE clause, use = instead of LIKE, it's more efficient:

UPDATE st SET seats = 'H25/27RO' WHERE body = 'Thomas Tilling'

Thank you very much that fixed it with ease.

 

Trouble for me is When  I google "mySQL + REPLACE"  every simple looking answer comes up with

UPDATE st SET seats = REPLACE(seats, 'H29/20R ', 'H25/27RO' ) WHERE body LIKE 'Thomas Tilling';

 

And the even simpler (I assume) global replace turns up

UPDATE st set seats = REPLACE(seats, 'H29/20R' , '(H27/25RO' )

 

Both you will notice use UPDATE and REPLACE in the queries, no wonder I hit so many hurdles when trying to find answers

If you could show me the correct syntax for a global replace that would also help a lot.

 

Regards  Topshed

the only thing that dictates which fields in the column are changed is the WHERE clause.  if there is no WHERE clause then all records will have their value updated in the column that you SET, so to perform a "global replace" as you call it simply leave off the WHERE.

UPDATE st SET seats = 'H25/27RO'

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.