PHP for learning PHP operation SQL One

Source: Internet
Author: User

A case for connecting to a database: the function used is

1.mysql_connect (' IP ', ' Database user name ', ' Database Password ') Open a connection to the MySQL server returns a resource handle
2.mysql_select_db (' Database name ') Select MySQL database
3.mysql_query (' query statement ')-send a MySQL query
4.mysql_fetch_array (' Result of query statement ', MySQL indexing method) takes a row from the result set as an associative array, or as a numeric array, or both.
Mysql_both, you will get an array that contains both associative and numeric indexes. Use MYSQL_ASSOC only to get associated index, use Mysql_num to get numeric index only
5.mysql_free_result (query result)-free result memory
6.mysql_close-Close MySQL Connection
Or Die (". Mysql_error ()) throws an error

<?php//page encoding Format header (' Content-type:text/html;charset=utf-8 ');//Open Database server define (' db_host ', ' localhost ');d efine ( ' Db_user ', ' root ');d efine (' db_pwd ', ' root ');d efine (' db_name ', ' school '); [Email Protected]_connect (db_host,db_user,db_pwd) or Die (' Database connection failed '. Mysql_error ());//Select the database @mysql_select_db to be used (db _name) or Die  (' database does not exist '. Mysql_error ());//conversion with Database encoding @mysql_query (' SET NAMES UTF8 ') or Die (' coding error '. Mysql_error ());// Write the query statement $query= ' Select*from grade ';//execute the query statement to return the result [email protected]_query ($query, $conn) or Die (' Execute statement error '). Mysql_error ()) ;//Print out the execution result Print_r (mysql_fetch_array ($result, MYSQL_ASSOC));//Release Resource mysql_free_result ($result);//Close Database Mysql_close ( $conn);? >

  

PHP for learning PHP operation SQL One

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.