SQL Replace replace field middle part specified content

Source: Internet
Author: User
Tags mysql tutorial create database

Create a database tutorial

CREATE database cnnet;

Creating data Tables

CREATE TABLE ' cnnet '. ' Test ' (
' ID ' INT (4) Not NULL auto_increment,
' URL ' VARCHAR NULL,
PRIMARY KEY (' id ')
) ENGINE = MYISAM

Insert a few data:

INSERT into ' test ' (' id ', ' url ') VALUES
(1, ' www.1aa.cm '),
(2, ' www.1aa.cm '),
(3, ' www.1aa.cm '),
(4, ' www.1aa.cm '),
(5, ' www.111cn.net '),
(6, ' www.111cn.net ');

Now let's take a look at the introductory tutorial.

Creating a database connection
*/

$cn = mysql tutorial _connect (' 127.0.0.1 ', ' root ', ' root ') or Die (' Database connection failed ');
$sql = "SELECT * from test where URL like '%1aa.cm% '";
$result = Mysql_db_query (' cnnet ', $sql, $CN);
while ($rs = Mysql_fetch_array ($result))
{
echo $rs [' url '], ' <br/> ';
}

/*
Output results
www.1aa.cm
www.1aa.cm
www.1aa.cm
www.1aa.cm
The good result comes out, we formally carry on the topic, replace replaces the content, we request to replace the 1aa.cm all to 111cn.net
*/

$sqlReplace = "Update test set URL = replace (' 1aa.cm ', ' 111cn.net ', url) WHERE url like '%1aa.cm% '";
Mysql_db_query (' cnnet ', $sqlReplace, $CN);
if (1)
{
$sql = "SELECT * from Test";
$query = Mysql_db_query (' cnnet ', $sql, $CN);
while ($row = Mysql_fetch_array ($query))
{
echo $rs [' url '], ' <br/> ';
}
}
Else
{
echo ' Check your SQL statement, thank you ';
}

/*
Output results:

www.111cn.net
Www.111cn.net
111cn.net
111cn.net
111cn.net
111cn.net

Note the original article reproduced in this site and indicate the source www.111cn.net
*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.