PHP interacts with MySQL

Source: Internet
Author: User
Tags php and mysql
PHP interacts with MySQL

A simple example of the code logic is: Make a connection, create a table, insert data, get the data, and display the results.

(1): Command line Build test database

(2): The following is a PHP script ( to connect, create a table, insert data, get data, and display the results)

 A record has been inserted. "; /query Record $sql_2= "SELECT * from TestTable", $res _2=mysqli_query ($mysqli, $sql _2), if ($res _2) {//show number of record bars $number_of_rows = Mysqli_num_rows ($res _2);p rintf ("
Result set has%d rows.\n ", $number _of_rows);//Gets data and displays the result while ($newArray = Mysqli_fetch_array ($res _2, Mysqli_assoc)) {$ id = $newArray [' id ']; $testField = $newArray [' Testfield '];echo "
The ID is ". $id." And the text is ". $testField."
";}} else {printf ("Could not retrieve records:%s\n", Mysqli_error ($mysqli));}} else {printf ("Could not insert Record:%s\n", Mysqli_error ($mysqli));}} else {printf ("Could not create table:%s\n", Mysqli_error ($mysqli));} Mysqli_close ($MYSQLI);}

Through server access, the effect is as follows:

Go to the command line to view the database

    • Size: 176.6 KB
    • Size: 20.4 KB
    • Size: 145.7 KB
    • View Picture Attachments

The above describes the PHP and MySQL interaction, including aspects of the content, I hope the PHP tutorial interested in a friend to help.

  • 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.