Hongmu Notes
Home Language Notes PHP moves the contents of one table to another table, provided that the table fields are identical.
Language Notes PHP PHP and mysql

PHP moves the contents of one table to another table, provided that the table fields are identical.

PHP moves the contents of one table to another table, provided that the table fields are identical.

If you want to move the contents of one table to another table whose fields are exactly the same, use the following steps:

  1. Connect to a MySQL database and select the database you want to operate on.
    $db = mysqli_connect("localhost", "username", "password", "my_database");
    mysqli_select_db($db, "my_database");
  2. Select the data to move from the source table. You can use SELECT statement to select the data to move, as follows:
    $result = mysqli_query($db, "SELECT * FROM source_table WHERE condition");

    In the above statement,source_table is the table name of the source table,condition is the condition for selecting data.

  3. Inserts selected data into the target table. You can use INSERT INTO The statement inserts data into the target table as follows:
    while ($row = mysqli_fetch_assoc($result)) {
      $values = implode("', '", $row);
      mysqli_query($db, "INSERT INTO target_table VALUES ('$values')");
    }

    In the above statement,target_table is the table name of the target table,$row is an associative array of one row of data,$values Is a string of array values ​​separated by commas. Since the table fields are exactly the same, we can directly use INSERT INTO statement to insert data into the target table.

  4. Close the database connection.
    mysqli_close($db);

The complete PHP code looks like this:

$db = mysqli_connect("localhost", "username", "password", "my_database");
mysqli_select_db($db, "my_database");

$result = mysqli_query($db, "SELECT * FROM source_table WHERE condition");

while ($row = mysqli_fetch_assoc($result)) {
  $values = implode("', '", $row);
  mysqli_query($db, "INSERT INTO target_table VALUES ('$values')");
}

mysqli_close($db);

In the above code you need to put source_tabletarget_table with the actual table name and replace condition Replace with a condition for selecting the data. In addition, please note that the code needs to be adjusted and optimized according to the specific situation in the actual use.

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

PHP cast type

PHP cast type Language Notes PHP PHP collection PHP and mysql

Get the data type 1. If you want to check the value and type of an expression, use var_dump(). 2. If you just want to get an easy-to-read type expression for debugging, use gettype(). 3. To check a certain type, do not use gettype(), but use the is_type() function. Converting Strings to Numbers When a string is evaluated as a number, the result is determined according to the following rules...
👁 232
PHP special character escaping and restoration

PHP special character escaping and restoration Language Notes PHP PHP collection PHP and mysql

Escape character is a special character constant. Escape characters are backslashed " & quot; At the beginning, followed by one or more characters. The escaped character has a specific meaning, which is different from the original meaning of the character, so it is called "escaped" character. The use of escape characters 1: turn ordinary characters into special purposes, such as back key and enter key. 2. Used to convert a character with special meaning back to its original meaning. 3. Before data is written into the database, escape characters (function …
👁 180
mysqli in PHP

mysqli in PHP Language Notes PHP PHP collection PHP and mysql

The `mysqli_num_rows()` function is exclusively used with `SELECT` query methods, whereas the `mysqli_affected_rows()` function returns the number of rows affected by the previous SQL statement across the entire database; this function is primarily used with `INSERT`, `UPDATE`, and `DELETE` operations.
👁 145

Recommended reading

Responsive financial management services management website template 0383

Responsive financial management services management website template 0383 Practical Collection Yiyou template

This set of eyoucms responsive template is suitable for the financial management service management industry. The design style is professional finance, and it can show financial products, investment services, wealth management and successful cases. It helps financial institutions show their brands online and attract investors. Template display, installation instructions Website background: / login.php account: admin password: admin related articles of SMS CMS installation FAQ summary of SMS CMS (E...
👁 38
(PC + WAP) Home Textiles – Knitted Bedding and Daily Care Products – Corporate Website (PBootCMS Template) – Download – 0712

(PC + WAP) Home Textiles – Knitted Bedding and Daily Care Products – Corporate Website (PBootCMS Template) – Download – 0712 Practical Collection pbootcms Template

A PbootCMS template specifically designed for home textile, bedding, and daily household goods companies, supporting access via both PC and WAP devices. Its cozy, lifestyle-oriented design is ideal for showcasing products such as bedding, towels, and everyday household items. This template helps home goods companies quickly establish a branded official website and expand their online retail or wholesale distribution channels. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin...
👁 50
Responsive housing rental and sale property company website template 0384

Responsive housing rental and sale property company website template 0384 Practical Collection Yiyou template

A eyoucms responsive website template for housing rental, sales and property companies. Design style of professional real estate, can show the real estate rental and sales information, property services, enterprise strength and brand image. It helps real estate companies attract home buyers and tenants online. Template display, installation instructions website background: / login.php account: admin password: admin related articles easy CMS installation FAQ summary easy CM...
👁 42
(Adaptive Mobile Version) Responsive Spectrometer Electronic Instrumentation Company – PBootCMS Website Template; Download Source Code for Precision Instrumentation Websites – 0713

(Adaptive Mobile Version) Responsive Spectrometer Electronic Instrumentation Company – PBootCMS Website Template; Download Source Code for Precision Instrumentation Websites – 0713 Practical Collection pbootcms Template

A responsive website template for an optical spectrometer and electronic instrumentation company (PbootCMS), compatible with both PC and WAP devices. The design features a modern, technical aesthetic, making it ideal for showcasing high-end equipment such as spectrometers and analytical instruments. This template helps precision instrument manufacturers showcase their product performance and technical specifications online, attracting customers from both the scientific research and industrial sectors. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extract...
👁 42
Acupuncture Notes [Human Chronicles] Chapter 1

Acupuncture Notes [Human Chronicles] Chapter 1 Hobbies Traditional Chinese Medicine Terminology

When to use acupuncture. Contraindications: long-term illness with severe deficiency of vital energy and blood, no need for acupuncture after sexual intercourse, no need for strenuous exercise, profuse sweating, no need for drinking, drunkenness, no need for acupuncture. It can be used for all diseases. Mugwort: It has a downward nature. [In ancient times, troops looked for water and used mugwort.] When pure Yang acupuncture does not work, or when the vitality, qi, and blood are severely deficient, moxibustion can be used. The method of moxibustion is ginger-partitioned moxibustion [ginger, sliced, punched, strong = portion, unit, single moxibustion, not double moxibustion, odd number 3579 positive number] salt-partitioned moxibustion [salt bar (sea salt...
👁 213
Responsive stationery supplies puzzle toy website template 0385

Responsive stationery supplies puzzle toy website template 0385 Practical Collection Yiyou template

This set of eyoucms responsive template is suitable for stationery and educational toy industry. The design style is lively and practical, and can display stationery products, educational toys, brand stories and educational concepts. It helps stationery and toy brands to display their products online and attract parents and students. Template display, installation instructions website background: / login.php account: admin password: admin related article easy you CMS installation FAQ summary easy you...
👁 45