Home> Topics> php mysql> body text

How to query mysql file in php

藏色散人
Release: 2020-09-12 09:37:42
Original
1443 people have browsed it

How to query mysql files in php: first use the "mysql_connect" function to connect to the mysql database; then select the specified mysql database through "mysql_select_db"; finally use the "mysql_query" method to query.

How to query mysql file in php

Recommended tutorial: "php mysql"

PHP connects to MySQL database

Connecting to the database

Copy after login

We use double exclamation marks!! to convert the resource handle into a Boolean value, and output 1 if it is correct, and an error message if it is incorrect. If the @ symbol is added in front, the error message will be ignored and no error message will be output.

For error message processing, we can use the mysql_error() function to output the error message:

mysql_connect('localhost','root','****') or die( 'Database connection failed, error message: '.mysql_error()); // Tips for password errors: Database connection failed, error message: Access denied for user 'root'@'localhost' (using password: YES)

die() function outputs a message and exits the current script. This function is an alias for the exit() function.

Database connection parameters can be stored as constants, so they cannot be modified at will and are safer.

 
Copy after login

It is worth noting that the constants in the brackets of mysql_connect() cannot be quoted, otherwise an error will occur.

Select the specified database

Copy after login

Usually there is no need to use mysql_close(), because the opened non-persistent connection will be automatically closed after the script is executed

mysql_select_db(database,connection): Select the MySQL database

Get the record set

 
Copy after login

mysql_query() function executes a MySQL query.

Output data

 
Copy after login

Release result set resources (only needs to be called when considering how much memory will be occupied when returning a large result set.)

Copy after login

The above is the detailed content of How to query mysql file in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!