Home  >  Article  >  Backend Development  >  PHP and MYSql connection and query

PHP and MYSql connection and query

高洛峰
高洛峰Original
2016-11-30 13:14:47975browse

1. Connect to the database
1. mysql_connect: Open the MySQL link

mysql_connect('host', 'username', 'password')

2. mysql_select_db: open a database
mysql_select_db('database name', $ link identifier character) //If the link identifier is not filled in, it will default to the last opened connection

3. mysql_query("set names 'GBK'") solves the Chinese garbled problem;

 mysql_query("set names 'encoding (utf8 or GBK) ' ") //UTF8 cannot have "-"

2. Query data

1. mysql_query (SQL statement, connection identifier);

$sql="Select * FROM Test "
$result=mysql_query($sql ) //The connection identifier defaults to the last opened link

//Get error information

$result=@mysql_query($sql) or die(mysql_error())

2. Get the query results

 a , mysql_fetch_row($result);

  $row=mysql_fetch_row($result);
   echo $row[0];

b、 mysql_fetch_array($result); sult);
echo $ row[0];
echo $row['key'];


Explanation: 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 ​​of the next row and saves them in an array. Returns false if there is no row.

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