Home > Topics > php mysql > How to query mysql file in php

How to query mysql file in php

藏色散人
Release: 2020-09-12 09:37:42
Original
1623 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

<?php
    header(&#39;COntent-Type:text/html;charset=utf-8&#39;);//设置页面编码,如果文件是gbk编码,则charset也应用gbk
    //@表示如果出错了,不要报错,直接忽略
    //参数:服务器地址,用户名和密码
    echo (!!@mysql_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;*****&#39;));//1
?>
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.

<meta charset="utf-8">
<?php
    //定义常量参数
    define(&#39;DB_HOST&#39;,&#39;localhost&#39;);
    define(&#39;DB_USER&#39;,&#39;root&#39;);
    define(&#39;DB_PWD&#39;,&#39;345823&#39;);//密码
    $connect = mysql_connect(DB_HOST,DB_USER,DB_PWD) or die(&#39;数据库连接失败,错误信息:&#39;.mysql_error());
    echo $connect;//Resource id #2 
?>
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

<?php
    define(&#39;DB_HOST&#39;,&#39;localhost&#39;);
    define(&#39;DB_USER&#39;,&#39;root&#39;);
    define(&#39;DB_PWD&#39;,&#39;345823&#39;);//密码
    define(&#39;DB_NAME&#39;,&#39;trigkit&#39;);//在phpmyadmin创建一个名为trigkit的数据库
    //连接数据库
    $connect = mysql_connect(DB_HOST,DB_USER,DB_PWD) or die(&#39;数据库连接失败,错误信息:&#39;.mysql_error());
    //选择指定数据库
    mysql_select_db(DB_NAME,$connect) or die(&#39;数据库连接错误,错误信息:&#39;.mysql_error());//将表名字故意写错,提示的错误信息:数据库连接错误,错误信息:Unknown database &#39;trigkt&#39;
?>
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

<meta charset="utf-8">
<?php
    define(&#39;DB_HOST&#39;,&#39;localhost&#39;);
    define(&#39;DB_USER&#39;,&#39;root&#39;);
    define(&#39;DB_PWD&#39;,&#39;345823&#39;);//密码
    define(&#39;DB_NAME&#39;,&#39;trigkit&#39;);
    //连接数据库
    $connect = mysql_connect(DB_HOST,DB_USER,DB_PWD) or die(&#39;数据库连接失败,错误信息:&#39;.mysql_error());
    //选择指定数据库
    mysql_select_db(DB_NAME,$connect) or die(&#39;数据表连接错误,错误信息:&#39;.mysql_error());
    //从数据库里把表的数据提出来(获取记录集)
    $query = "SELECT * FROM class";//在trigkit数据库中新建一张&#39;表&#39;
    $result = mysql_query($query) or die(&#39;SQL错误,错误信息:&#39;.mysql_error());//故意将表名写错:SQL错误,错误信息:Table &#39;trigkit.clas&#39; doesn&#39;t exist
?>
Copy after login

mysql_query() function executes a MySQL query.

Output data

<meta charset="utf-8">
<?php
    define(&#39;DB_HOST&#39;,&#39;localhost&#39;);
    define(&#39;DB_USER&#39;,&#39;root&#39;);
    define(&#39;DB_PWD&#39;,&#39;345823&#39;);//密码
    define(&#39;DB_NAME&#39;,&#39;trigkit&#39;);
    //连接数据库
    $connect = mysql_connect(DB_HOST,DB_USER,DB_PWD) or die(&#39;数据库连接失败,错误信息:&#39;.mysql_error());
    //选择指定数据库,设置字符集
    mysql_select_db(DB_NAME,$connect) or die(&#39;数据表连接错误,错误信息:&#39;.mysql_error());
    mysql_query(&#39;SET NAMES UTF8&#39;) or die(&#39;字符集设置出错&#39;.mysql_error());
    //从数据库里把表的数据提出来(获取记录集)
    $query = "SELECT * FROM class";
    $result = mysql_query($query) or die(&#39;SQL错误,错误信息:&#39;.mysql_error());
    print_r(mysql_fetch_array($result,MYSQL_ASSOC));
?>
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.)

<?php
    mysql_free_result($result); 
?>
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template