PHP entry-php operation mysql database example code_PHP tutorial

WBOY
Release: 2016-07-13 17:13:32
Original
799 people have browsed it

This is a simple introductory article on querying data with PHP and MySQL. We have talked about the most important functions and introduced them in detail. Students in need can take a look.

The code is as follows
 代码如下 复制代码

//链接数据库

$conn = @mysql_connect("localhost","root","88888888") or die ("链接错误");

/*
mysql_connect() 函数打开非持久的 MySQL 连接。

Copy code



//Link database

$conn = @mysql_connect("localhost","root","88888888") or die ("Link Error");

/*
The mysql_connect() function opens a non-persistent MySQL connection.

Grammar
 代码如下 复制代码

 //解决中文乱码

mysql_query("set names 'GBK'"); 

//打开数据库
mysql_select_db("wordpress",$conn) or die("打开失败");
$sql = "select * from wp_posts";

mysql_connect(server,user,pwd,newlink,clientflag) parameter description

server is optional. Specifies the server to connect to.

Can include a port number, such as "hostname:port", or a path to a local socket, such as ":/path/to/socket" for localhost.

If the PHP directive mysql.default_host is not defined (the default case), the default value is 'localhost:3306'.

user optional. username. The default value is the username of the server process owner.
pwd Optional. password. The default value is an empty password.

*/

The mysql_select_db() function sets the active MySQL database.
The code is as follows
 代码如下 复制代码

//执行sql语句
$result = mysql_query($sql,$conn);

//循环打印所需参数
while($row = mysql_fetch_array($result))
{
    echo $row[4]."
";//测验多次,$row[4]中只能为索引,不能为列名,郁闷中ing
}

Copy code

//Solve Chinese garbled characters

mysql_query("set names 'GBK'");

//Open database
mysql_select_db("wordpress",$conn) or die("Open failed");

$sql = "select * from wp_posts";



If successful, the function returns true. If failed, returns false.

Grammar

mysql_select_db(database,connection) parameter description connection is optional. Specifies the MySQL connection. If not specified, the previous connection is used.
The code is as follows Copy code
//Execute sql statement $result = mysql_query($sql,$conn); //Loop to print the required parameters while($row = mysql_fetch_array($result)) { echo $row[4]."
";//Tested many times, $row[4] can only be an index, not a column name, I am depressed
}
The mysql_fetch_array() function fetches a row from the result set as an associative array, a numeric array, or both Returns an array based on the rows taken from the result set, or false if there are no more rows. Grammar mysql_fetch_array(data,array_type) parameter description data optional. The specification specifies the data pointer to be used. This data pointer is the result of the mysql_query() function. array_type optional. Specifies what kind of results are returned. Possible values: MYSQL_ASSOC - Associative array MYSQL_NUM - array of numbers MYSQL_BOTH - Default. Produce both associative and numeric arrays http://www.bkjia.com/PHPjc/629152.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629152.htmlTechArticleThis is a simple introductory article on querying data with php and mysql. We have talked about the most important points. Each function is introduced in detail, students who need it can take a look. The code is as follows...
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