PHP operation MySQL database method sharing in Mac environment, macmysql_php tutorial

Source: Internet
Author: User
Tags mysql host sql error

PHP operation MySQL database method sharing in Mac environment, Macmysql


MAC Local Environment Setup

On Mac systems, we can use Mamp Pro software to build local servers. Install the software, the site directory in the/applications/mamp/htdocs folder, just put the file into the folder, you can access through the http://localhost:8888, or by clicking the Red Underline button below to quickly access the site.

The Mac system installs PHP, two lines can.

Brew Tap Josegonzalez/homebrew-phpbrew Install PHP54

After installation, you can use Phpstorm to program happily. Installed PHP path in/usr/local/bin/php

Basic Database Operations

1) The user's Web browser makes an HTTP request requesting a specific Web page.
2) The Web server receives the. PHP request to get the file and upload it to the PHP engine to request it to process. 3) The PHP engine starts parsing the script. The script has a command to connect to the database, and an order to execute a query. Life
PHP opens the connection to the MYSQL database and sends the appropriate query.
4) The MYSQL server receives database queries and processes them. Returns the result to the PHP engine.
5) PHP Where do you go? Complete the script run, which typically includes formatting the query results in HTML format. Then
Then output the HTML back to the WEB server.
6) The Web server sends HTML to the browser.
MySQL Common data types

Integer type: tinyint,smallint,int,bigint
Float type: Floa T,doub le,decimal (m,d)
Character Type: Char,varchar
Date type: DA tetime,da te,timesta MP
Remark Type: Tinytext,text,longtext
MySQL Database Operations

1) Display the currently existing database
>SHOWDATABASES;
2) Select the database you need
>USEguest;
3) View the currently selected database
>selectdatabase ();
4) View all the contents of a single table
>SELECT*FROMguest; You can go through showtables first to see how many tables there are.
5) Set the Chinese code according to the database
>set NAMESGBK; Set names UTF8;
6) Create a database
>CREATEDATABASEbook;
7) Create a table in the database
>createtableusers (
>username VARCHAR,//not null setting is not allowed to be empty
>sex CHAR (1),
>birth DATETIME);
8) Display the structure of the table
>DESCIRBEusers;

9) Insert a piece of data into the table

>insert into the Users (Username,sex,birth) VALUES (' Jack ', ' Male ', now ());

PHP connection MySQL Database

Connecting to a database

<?php  Header (' content-type:text/html;charset=utf-8 ');//Set page encoding, if the file is GBK encoded, then CharSet also applies gbk  //@ to indicate that if something goes wrong, Do not error, directly ignore  //Parameters: Server address, user name and password  

We use double exclamation marks!! To convert the resource handle to a Boolean value, output 1 correctly, and error output error message. If the @ symbol is preceded, the error message is ignored and the error message is not printed.

For the handling of error messages, we can use the mysql_error () function to output error messages:

mysql_connect (' localhost ', ' root ', ' * * * ') or Die (' Database connection failed, error message: '. mysql_error ());//hint for password error: Database connection failed, error message: Access Denied for user ' root ' @ ' localhost ' (using Password:yes)
The die () function outputs a message and exits the current script. The function is an alias for the exit () function.

Database connection parameters can be stored with constants so that they cannot be arbitrarily modified and more secure.

 
  <?php  //define constant parameter  define (' db_host ', ' localhost ');  Define (' Db_user ', ' root ');  Define (' Db_pwd ', ' 345823 ');//password  $connect = mysql_connect (db_host,db_user,db_pwd) or Die (' Database connection failed, error message: '. Mysql_ Error ());  

It is important to note that the constants in mysql_connect () brackets cannot be quoted, otherwise there must be an error.

Select the specified database

<?php  define (' db_host ', ' localhost ');  Define (' Db_user ', ' root ');  Define (' Db_pwd ', ' 345823 ');//password  define (' db_name ', ' trigkit ');//Create a database  //Connection database  named phpMyAdmin in Trigkit $connect = mysql_connect (db_host,db_user,db_pwd) or Die (' Database connection failed, error message: '. mysql_error ());  Select the specified database  

Mysql_close () is not usually required because an open non-persistent connection automatically shuts down after the script has finished executing

mysql_select_db (database,connection): Select MySQL Database

Get record set

 
  <?php  define (' db_host ', ' localhost ');  Define (' Db_user ', ' root ');  Define (' Db_pwd ', ' 345823 ');//password  define (' db_name ', ' Trigkit ');  Connection database  $connect = mysql_connect (db_host,db_user,db_pwd) or Die (' Database connection failed, error message: '. mysql_error ());  Select the specified database  mysql_select_db (db_name, $connect) or Die (' data table connection error, error message: '. mysql_error ());  The data from the table is raised from the database (get Recordset)  $query = "SELECT * from Class";//Create a new ' table ' in the Trigkit database  

The mysql_query () function executes a MySQL query.

Output data

 
  <?php  define (' db_host ', ' localhost ');  Define (' Db_user ', ' root ');  Define (' Db_pwd ', ' 345823 ');//password  define (' db_name ', ' Trigkit ');  Connection database  $connect = mysql_connect (db_host,db_user,db_pwd) or Die (' Database connection failed, error message: '. mysql_error ());  Select the specified database, set the character set  mysql_select_db (db_name, $connect) or Die (' data table connection error, error message: '. mysql_error ());  mysql_query (' Set NAMES UTF8 ') or Die (' CharSet set error '. Mysql_error ());  The data from the table is raised from the database (get Recordset)  $query = "SELECT * from class";  $result = mysql_query ($query) or Die (' SQL error, error message: '. mysql_error ());  

Frees the result set resource (called only if you want to take into account how much memory is consumed when a large result set is returned. )

<?php  mysql_free_result ($result);?>

Change and delete

New data

<?php  require ' index.php ';  Added data  $query = "INSERT into CLASS (       name, E       -Mail, point       ,       regdate)    VALUES (    ' Xiao Ming ',    ' Xiaoming@163.com ', +    , now    ()    ) ";  @mysql_query ($query) or Die (' New error: '. mysql_error ());? >

We save the above code as index.php and throw it into the/applications/mamp/htdocs/folder. Save the above code as demo.php and put it in the same directory. Mac system gets the path to the file is simple, just pull the file into the terminal to display the path name.

modifying data

We assume that the name of the data to be modified is xiaoming, ID 2, change his point score to 80 points, the code is as follows:

<?php  require ' index.php ';  Modify data  $query = ' UPDATE class SET point=80 WHERE id=2 ';  @mysql_query ($query);? >

Delete data

<?php  require ' index.php ';  Delete data  $query = "Delete from class WHERE id=2";  @mysql_query ($query);  Mysql_close ();? >

Show data

<?php  require ' index.php ';  Display data  $query = "Select Id,name,email,regdate from class";  $result = mysql_query ($query) or Die (' SQL statement error: '. mysql_error ());  Print_r (Mysql_fetch_array ($result));  Mysql_close ();? >

or display the specified value data:

$data = Mysql_fetch_array ($result); Echo $data [' email '];//display emailecho $data [' name '];//display name

Other common functions

Copy the Code code as follows:
Mysql_fetch_lengths (): Gets the length of each output in the result set
Mysql_field_name (): Gets the field name of the specified field in the result

MySQL _fetch_row (): Get a row from the result set as an enumerated array
MYSQL_FETCH_ASSOC (): Gets a row from the result set as an associative array
Mysql_fetch_array (): Gets a row from the result set as an associative array, or as a numeric array, or both

Mysql_num_rows (): Gets the number of rows in the result set
Mysql_num_fields (): Gets the number of fields in the result set

Mysql_get_client_info (): Get MySQL Client Information
Mysql_get_host_info (): Get MySQL host information
Mysql_get_proto_info (): Get MySQL protocol information
Mysql_get_server_info (): Get MySQL Server information

The above mentioned is the whole content of this article, I hope you can like.

http://www.bkjia.com/PHPjc/997908.html www.bkjia.com true http://www.bkjia.com/PHPjc/997908.html techarticle mac Environment PHP operation MySQL Database method sharing, Macmysql Mac Local environment built on Mac system, we can use Mamp Pro software to build local server. Install this software, ...

  • Related Article

    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.