PHP operation MySQL Database method sharing _php tutorial in Mac Environment

Source: Internet
Author: User
Tags mysql host mysql query sql error

How to share PHP operation MySQL database in Mac environment


Today on the Mac set up the PHP environment, we will PHP operation MySQL database method to share to everyone, the need for a small partner reference.

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.

?

1

2

Brew Tap josegonzalez/homebrew-php

Brew 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

?

1

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

PHP connection MySQL Database

Connecting to a database

?

1

2

3

4

5

6

7

Header (' content-type:text/html;charset=utf-8 ');//Set page encoding, if the file is GBK encoded, then CharSet also applies GBK

@ indicates that if there is an error, do not error, directly ignore

Parameters: Server address, user name and password

Echo (!! @mysql_connect (' localhost ', ' root ', ' * * * * *) ');//1

?>

We use double exclamation marks!! To convert the resource handle to a Boolean value, output 1 correctly, and error output error message. And if you precede the @ symbol,

The error message is ignored and no error message is output.

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

?

1

2

3

4

5

6

7

8

9

Defining constant parameters

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 ());

echo $connect;//resource ID #2

?>

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

?

1

2

3

4

5

6

7

8

9

10

Define (' db_host ', ' localhost ');

Define (' Db_user ', ' root ');

Define (' Db_pwd ', ' 345823 ');//password

Define (' db_name ', ' trigkit ');//Create a database named Trigkit in phpMyAdmin

Connecting to a 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 (' Database connection error, error message: '. mysql_error ());//write the table name deliberately wrong,

Tip Error Message: Database connection error, error message: Unknown ' trigkt '

?>

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Define (' db_host ', ' localhost ');

Define (' Db_user ', ' root ');

Define (' Db_pwd ', ' 345823 ');//password

Define (' db_name ', ' trigkit ');

Connecting to a 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 ());

Bring the table data from the database (get the Recordset)

$query = "SELECT * from Class";//Create a new ' table ' in the Trigkit database

$result = mysql_query ($query) or Die (' SQL error, error message: '. mysql_error ());//deliberately write the table name incorrectly: SQL error, error message: Table ' Trigkit.clas ' doesn ' t Exist

?>

The mysql_query () function executes a MySQL query.

Output data

?

1

2

3

4

5

6

7

8

9

Ten

One

Up

!--? php

Define (' db_host ', ' localhost ');

Define (' Db_user ', ' root ');

Define (' db_pwd ', ' 345823 ');//password

Define (' db_name ', ' Trigkit ');

//Connect to 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 CharSet

mysql_select_db (db_name, $connect) or Die (' data table connection error, error message: '. mysql_error ());

mysql_query (' Set NAMES UTF8 ') or Die (' character set setting error '. Mysql_error ());

//Put data from the table (get Recordset) from the database

$query = "SELECT * from class";

$result = mysql_query ($query) or Die (' SQL error, error message: '. mysql_error ());

Print_r (mysql_fetch_array ($result, MYSQL_ASSOC));

?

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

?

1

2

3

Mysql_free_result ($result);

?>

Change and delete

New data

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

Require ' index.php ';

New data

$query = "INSERT into CLASS (

Name

Email

Point

RegDate)

VALUES (

' Xiao Ming ',

' Xiaoming@163.com ',

100,

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,

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:

?

1

2

3

4

5

6

7

Require ' index.php ';

modifying data

$query = ' UPDATE class SET point=80 WHERE id=2 ';

@mysql_query ($query);

?>

Delete data

?

1

2

3

4

5

6

7

8

9

Require ' index.php ';

Delete data

$query = "DELETE from class WHERE id=2";

@mysql_query ($query);

Mysql_close ();

?>

Show data

?

1

2

3

4

5

6

7

8

9

10

Require ' index.php ';

Show 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:

?

1

2

3

$data = Mysql_fetch_array ($result);

echo $data [' email '];//Show email

echo $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

http://www.bkjia.com/PHPjc/998355.html www.bkjia.com true http://www.bkjia.com/PHPjc/998355.html techarticle mac Environment PHP operation MySQL Database method sharing today on the Mac set up the PHP environment, we will PHP operation MySQL database method to share to everyone, the need for small partners reference ...

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