Home  >  Article  >  Backend Development  >  How to connect query between php and mysql

How to connect query between php and mysql

藏色散人
藏色散人Original
2020-07-25 09:13:102477browse

How to connect and query php with mysql: first use "mysql_connect" to connect to MySQL; then use "mysql_select_db" to open a database; finally use the "mysql_query" function to query data.

How to connect query between php and mysql

Recommended: "PHP Tutorial"

PHP and MYSql connection and query

1. Connect to the database

1. mysql_connect: Open the MySQL link

mysql_connect('主机','用户名','密码')

2. mysql_select_db: Open a database

mysql_select_db('数据库名',$链接标识符) //链接标识符不填写则默认为上一次打开的连接

3. mysql_query("set names ' GBK'") Solve the problem of Chinese garbled characters;

mysql_query("set names '编码(utf8或GBK)' ") //UTF8不能有“-”

2. Query data

1. mysql_query (SQL statement, connection identifier);

$sql="Select * FROM Test "
  $result=mysql_query($sql) //连接标识符默认为上一次打开的链接
  //获取错误信息
  $result=@mysql_query($sql) or die(mysql_error())

2. Get the query Result

a、 mysql_fetch_row($result);
  $row=mysql_fetch_row($result);
  echo $row[0];
b、 mysql_fetch_array($result);
  $row=mysql_fetch_array($result);
  echo $row[0];
  echo $row['key'];

Description: The functions of mysql_fetch_array and mysql_fetch_row are basically the same, except that in addition to using the offset starting from 0 as an index, it can also use the domain name as an index.

Value returns all field values ​​in the next row and saves them in an array. If there is no row, false is returned.

The above is the detailed content of How to connect query between php and mysql. 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