Common Php code for connecting to and reading and writing to the mysql database, and php for writing to the mysql database _ PHP Tutorial

Source: Internet
Author: User
Tags what php
Common code used by Php to connect to and read from and write to the mysql database. php writes to the mysql database. Php connects to, reads, and writes frequently used code to the mysql database. since php writes to the mysql database now you see this article, it means you must know what PHP and MySQL are like, I will not connect to and read and write common mysql database code in Php, and write php into mysql database

Now that you have read this article, you must know what PHP and MySQL are like. But why are you still reading this article? It may be that you used to copy and paste some code and didn't really understand the meaning of the code; or you may have understood it before, but like me, you have not been in touch for a while and are unfamiliar with it; or, someone asks you a simple question like this, and you are already waiting to answer the question. you just found this article by searching on the Internet, so I recommend it to someone who is there...

In any case, I have summarized the commonly used methods for connecting to the MySQL database using PHP and reading and writing data to the database. I hope this will help you. of course it is also a review and summary of my own.

1. to better set up data connections, the values involved in data connections are generally defined as variables.

$ Mysql_server_name = 'localhost'; // change it to your mysql database server $ mysql_username = 'root'; // change it to your mysql database username $ mysql_password = '123 '; // change your mysql database password $ mysql_database = 'mydb'; // change it to your mysql database name

You can also put the preceding variables in one file so that other files can be called at any time.

For example, if you place the preceding content in db_config.php, you can directly call it on other pages that require databases.

Call code: require ("db_config.php ");

2. connect to the database

$ Conn = mysql_connect ($ mysql_server_name, $ mysql_username, $ mysql_password) or die ("error connecting"); // connect to the database mysql_query ("set names 'utf8 '"); // The database output encoding must be consistent with your database encoding. nanchang site construction company baiheng Network PHP engineers are recommended to use UTF-8 international standard code. mysql_select_db ($ mysql_database); // open the database $ SQL = "select * from news"; // SQL statement $ result = mysql_query ($ SQL, $ conn); // query

3. read the content in the table. here we use while. you can use for or other.

While ($ row = mysql_fetch_array ($ result) {echo"

"; // Typographical code echo $ row ['topic ']."
"; Echo"

"; // Typographical code}

4. php writes data to the database and Mysql writes data.

$ Conn = mysql_connect ($ mysql_server_name, $ mysql_username, $ mysql_password); // connect to the database mysql_query ("set names 'utf8 '"); // database output code mysql_select_db ($ mysql_database); // open the database $ SQL = "insert into messageboard (Topic, Content, Enabled, Date) values ('$ Topic ', '$ content', '1', '1970-01-12') "; mysql_query ($ SQL); mysql_close (); // Close the MySQL connection


How can I use php code to write data to a database when connecting to mysql?

Connection: mysql_connect ("host", "user", "password ");
Mysql_select_db ("database name ");

Write Data: mysql_query ("insert into table name (Field 1, field 2) values (" Data 1 "," Data 2 ")");

Your question is too vague. I can only give you this answer and ask you again if you do not understand it.

Php reads txt text and writes it to the mysql database.

/*
Take data.txt as an example
When data is stored in a text document
Fputs ("data.txt", "$ str1 \ t $ str2 \ r \ n ");
In this form!
*/
$ Str = file_get_contents ("data.txt") or die ("file opening failed ");
$ R = explode ("\ r \ n", $ str );
/*
... Connect to the database
*/
$ SQL = "insert into 'Table' ('id', 'num') values ('". $ r [0]. "','". $ r [1]. "')";
Mysql_query ($ SQL );

// The above code has not been tested. if you have any questions, please contact me.
?>

Since you have read this article, you must know what PHP and MySQL are like...

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.