Web Hosting Talk







View Full Version : SQL query to find all field urls beginning with www?


lexington
12-17-2006, 02:30 AM
Hello, is there a mysql query that I can run that first finds and displays all of the data from the "url" field that begins with "www" and then another query that automatically adds http:// in front of the urls with www? The reason why is because I just noticed so many sites without the "http://" in the beginning which is causing 404 page errors. I plan to hve the script modifed later to automatically add that for future submissions. Thanks.

ub3r
12-17-2006, 04:13 AM
the query to find all the results would be:

select url from table where `url` = 'www%';

Not sure how you could do a mass-replace though. Have you considered just doing a mysqldump on your database, remove all instances of http:// from the dump file, clear out your table, then reload it with your http://less dump file?

It could even be done via server side, just mysql dump the db:

mysqldump -u username --password=yourpass databasename > dump.sql

open the .sql file in vi:

vi dump.sql

do the mass replace:

:%s'http://'';

save and close your dump file

:wq

login to mysql:

mysql -u username -p dbname

clear out your table:

delete from tablename;

exit mysql:

quit;

load the table back up with your new dump.sql file:

mysql -u username --password=yourpass dbname < dump.sql

and you're set.

lexington
12-17-2006, 04:16 AM
Thanks a lot :) *Edit* Hmm when I ran the command it resulted in zero results but I know that there are a ton of them.

select url from link where `url` = 'www%';

ub3r
12-17-2006, 04:19 AM
Forgot to add, you would then just dd in http:// to your script, so it always outputs with it.

Don't forget to str_replace http:// from your user input as well.

lexington
12-17-2006, 04:31 AM
Thanks again. You may not have noticed that I edited my last post, since I ran the query and it returned no results. However I know there are over 800 of them since I can search for a specific url beginning with www and not http://

ub3r
12-17-2006, 04:52 AM
sorry, correct syntax is

select url from link where `url` LIKE 'www%';

lexington
12-17-2006, 05:24 AM
Thanks. Actually the problem is solved now. This query fixes the problem:

UPDATE link SET url = CONCAT('http://', url) WHERE url LIKE 'www.%'

WO-Jacob
12-17-2006, 07:29 PM
Thanks. Actually the problem is solved now. This query fixes the problem:

UPDATE link SET url = CONCAT('http://', url) WHERE url LIKE 'www.%'

Actually, when you get a bunch of records in there, you'll want to do a key on that table, for the first 7 charactors and then do something like this:

UPDATE link SET url = CONCAT('http://', url) WHERE LEFT(url, 7) != 'http://'

That will be much more efficient and catch many more urls, as not everyone puts www. in front :)