PHP

REPLACE: Find and Replace Text in MySQL Database Using SQL

150 150 rhecht

The syntax of REPLACE is REPLACE(text_string, from_string, to_string)

update TABLE_NAME set FIELD_NAME = replace(FIELD_NAME, ‘find this string’, ‘replace found string with this string’);

update client_table set company_name = replace(company_name, ‘Old Company’, ‘New Company’)

Another example:


SELECT REPLACE(‘www.mysql.com’, ‘w’, ‘Ww’);

Above statement will return ‘WwWwWw.mysql.com’ as result.

How to Open CakePHP Files in Dreamweaver

150 150 rhecht

Thanks to James Fairhurst on this.

I primarily use Adobe Dreamweaver to create and code all of my websites, mainly because of the code highlighting and the way I organise my sites in separate folders. I realise that there are loads of text editors out there but Dreamweaver is what I’m comfortable with and it does the job.

When starting out with CakePHP I quickly found out that Dreamweaver doesn’t open .THTML and .CTP files automatically and I had to do a bit of hunting on the web to get the problem solved. The solution is pretty easy so this is just a quick post to get Dreamweaver opening .THTML and .CTP files.

THTML and CTP Files

Although I’m using CakePHP 1.1 which uses .THTML files I’ll also add the latest .CTP files which CakePHP 1.2 use. This is just saving time and when I do cross over and start using version 1.2 I don’t have to fiddle around with Dreamweaver again.

Modifying the Necessary Dreamweaver files

There are ultimately 3 files that you need to edit. The first two are located in the folder that you installed Dreamweaver, in my case this is “C:Program FilesAdobeAdobe Dreamweaver CS3configuration” although I’m using the latest version of Dreamweaver previous versions also have a “configuration” folder so go there.

Open up “Extensions.txt” and on the first line at the very end add THTML and CTP separated by commas, so the line should read:

,MASTER,THTML,CTP:All Documents

Similarly add these two extensions to the “:PHP Files” line.

PHP,PHP3,PHP4,PHP5,TPL,THTML,CTP:PHP Files

Next open the “DocumentTypes” folder and edit the “MMDocumentTypes.xml” file, just open it up using notepad or wordpad. Search for the line which has an id “PHP_MySQL” and add the THTML/CTP file extensions to both the “winfileextension” and “macfileextension” so the line should read:

winfileextension="php,php3,php4,php5,thtml,ctp"

macfileextension="php,php3,php4,php5,thtml,ctp"

The final file is another version of the “Extensions.txt” which is located in your “Documents and Settings” Folder in my case this is “C:Documents and SettingsJamesApplication DataAdobeDreamweaver 9Configuration” just add the very same things you inserted earlier.

Wrapping Up

Restart Dreamweaver and you should now be able to open up and edit .THTML and .CTP files with everything nicely colour coded. A short post but a needed one for those who use Dreamweaver.

Drupal – Restore Your Password for 6 and 7

150 150 rhecht

“Help!!! I need the admin password for a Drupal site!!!” Some people who get a Drupal site to test from their end don’t get password access. Here are a couple of ways to change a password.

Drupal 6 and 7


Update the email field for the admin user (user 1) in the “users” table. Unlike the password field, you can directly edit the email field without consequence. Once you’ve updated your email address for the admin user, simply go to the request password page to have your password emailed to your email address.

Drupal 6

source: (http://drupal.org/node/44164)

Since Drupal 6 and under is MD5 hash encoded, all you need to do is to execute the following query with direct database access:

UPDATE `users` SET pass = MD5(‘newpwd’) WHERE uid=1;

Drupal 7

1. source: (http://drupal.org/node/1023428)

Execute the following commands from the commandline, in the Drupal root directory:

./scripts/password-hash.sh newpwd or for Windows:

php .scriptspassword-hash.sh newpwd Of course, change “newpwd” to the desired password. If the password contains special characters such as a space, * or ? you must escape them, or wrap the password in quotes appropriate for the shell used.

The script will output a password hash that is valid for the site. Copy this to the clipboard or write it down somewhere; you’ll need it for the next step. Be careful not to include more or less characters as the hash. These hashes look somewhat like $S$CTo9G7Lx28rzCfpn4WB2hUlknDKv6QTqHaf82WLbhPT2K5TzKzML

Then execute the following query on the Drupal database:

UPDATE users SET pass =’thepasswordhash’ WHERE uid = 1; To execute this query it will be necessary to login to the database. This is typically done through the command line or through a GUI interface such as phpMyAdmin.

2. The user_hash_password() function, which, as its name suggests, generates a hash of your supplied string (this time using SHA512 and a salt). The output of this function is what should be used as the new password in the users table. For example, user_hash_password(123); returns $P$CD6Nf2aDgnBZZElo/teVOO2.h6sNT9/ as the hash. Replace the existing hash value in the database with this new hash (of a known string!) and you’re good to go!

3. Simplest way: Use the following Hash:

$S$C6x2r.aW5Nkg7st6/u.IKWjTerHXscjPtu4spwhCVZlP89UKcbb/

This is the hash code for the password (case sensitive – all uppercase letters):

“NEW_TEMP_PASSWORD”

That’s it!