Technology Programming

How to Replace Newlines in a MySQL String

    • 1). Open a terminal so you can work from the command line. If you are using phpMyAdmin or another tool that allows you to manipulate the database, open the tool that allows you to enter MySQL commands and skip to Step 4.

    • 2). Log in to MySQL by typing "mysql -u <username> -p <password>" and pressing "Enter."

    • 3). Open the database you want to work with by typing "connect <database name>" and pressing "Enter."

    • 4). Instruct MySQL to replace newline characters using the following query:

      update <table name> set <field name>=replace(<field name>, "\n", "<replacement string>");

      This will replace all instances of the newline character (identified by "\n") in all records of the table you specify. If you wish to limit the records affected, append a "WHERE" statement on the end of the query. See the MySQL documentation for more information on using WHERE.

    • 5). Replace undesired carriage returns that often accompany newline characters by running the previous command but using "\r" instead of "\n."



Leave a reply