search
HomeTopicsphp mysqlHow to query mysql file in php

How to query mysql file in php

Sep 12, 2020 am 09:37 AM
mysqlphp

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
?>

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 
?>

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;
?>

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
?>

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

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

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!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool