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.
Recommended tutorial: "php mysql"
PHP connects to MySQL database
Connecting to the database
<?php header('COntent-Type:text/html;charset=utf-8');//设置页面编码,如果文件是gbk编码,则charset也应用gbk //@表示如果出错了,不要报错,直接忽略 //参数:服务器地址,用户名和密码 echo (!!@mysql_connect('localhost','root','*****'));//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('DB_HOST','localhost'); define('DB_USER','root'); define('DB_PWD','345823');//密码 $connect = mysql_connect(DB_HOST,DB_USER,DB_PWD) or die('数据库连接失败,错误信息:'.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('DB_HOST','localhost'); define('DB_USER','root'); define('DB_PWD','345823');//密码 define('DB_NAME','trigkit');//在phpmyadmin创建一个名为trigkit的数据库 //连接数据库 $connect = mysql_connect(DB_HOST,DB_USER,DB_PWD) or die('数据库连接失败,错误信息:'.mysql_error()); //选择指定数据库 mysql_select_db(DB_NAME,$connect) or die('数据库连接错误,错误信息:'.mysql_error());//将表名字故意写错,提示的错误信息:数据库连接错误,错误信息:Unknown database 'trigkt' ?>
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('DB_HOST','localhost'); define('DB_USER','root'); define('DB_PWD','345823');//密码 define('DB_NAME','trigkit'); //连接数据库 $connect = mysql_connect(DB_HOST,DB_USER,DB_PWD) or die('数据库连接失败,错误信息:'.mysql_error()); //选择指定数据库 mysql_select_db(DB_NAME,$connect) or die('数据表连接错误,错误信息:'.mysql_error()); //从数据库里把表的数据提出来(获取记录集) $query = "SELECT * FROM class";//在trigkit数据库中新建一张'表' $result = mysql_query($query) or die('SQL错误,错误信息:'.mysql_error());//故意将表名写错:SQL错误,错误信息:Table 'trigkit.clas' doesn't exist ?>
mysql_query() function executes a MySQL query.
Output data
<meta charset="utf-8"> <?php define('DB_HOST','localhost'); define('DB_USER','root'); define('DB_PWD','345823');//密码 define('DB_NAME','trigkit'); //连接数据库 $connect = mysql_connect(DB_HOST,DB_USER,DB_PWD) or die('数据库连接失败,错误信息:'.mysql_error()); //选择指定数据库,设置字符集 mysql_select_db(DB_NAME,$connect) or die('数据表连接错误,错误信息:'.mysql_error()); mysql_query('SET NAMES UTF8') or die('字符集设置出错'.mysql_error()); //从数据库里把表的数据提出来(获取记录集) $query = "SELECT * FROM class"; $result = mysql_query($query) or die('SQL错误,错误信息:'.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!