Home > Backend Development > PHP Tutorial > PHP connection mysql example code_PHP tutorial

PHP connection mysql example code_PHP tutorial

WBOY
Release: 2016-07-13 10:46:29
Original
996 people have browsed it

This is an article that uses the mysql_connect function of php to connect to the mysql database. The prompt conditions require that your php has php_mysql.dll and libmysql.dll and php.ini; extension=php_mysql, remove the ";" in front of it. .

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

mysql_connect("localhost", "root","1981427") //连接位于localhost的服务器,用户名为root
?>

Copy code

 代码如下 复制代码

@mysql_select_db("test") //选择数据库mydb

?>

mysql_connect("localhost", "root","1981427") //Connect to the server located on localhost, the user name is root

?>

Connect and select the database

The code is as follows

Copy code

@mysql_select_db("test") //Select database mydb

?>

This way we can connect to the database

 代码如下 复制代码
$conn = @mysql_connect("localhost", "root", "root") or die("数据库链接错误");
mysql_select_db("data", $conn);//data为数据库名称
mysql_query("set names 'GBK'"); //使用GBK中文编码;
?>
Problems you may encounter


When running the code, an error message appears: Call to undefined function 'mysql_connect()'... failed

 代码如下 复制代码

include("conn.php");//引入conn.php文件
$SQL="SELECT * FROM `table` order by id desc";
$query=mysql_query($SQL);
while($row=mysql_fetch_array($query)){
?>
显示数据内容:

}
?>

This prompt function undefined means that there is no mysql_connect() function,

The solution is as follows


Copy the php_mysql.dll and libmysql.dll files to c:winntsystem32 (I missed libmysql.dll)

Find ;extension=php_mysql in php.ini and remove the ";" in front of it Restart the server

Okay, everything is ready, now we just need a simple connection instance

Example 1

The index.php file code is as follows:
The code is as follows Copy code
include("conn.php");//Introduce the conn.php file $SQL="SELECT * FROM `table` order by id desc"; $query=mysql_query($SQL);

while($row=mysql_fetch_array($query)){ ?>
Display data content:
}<🎜> ?> In this way we can implement a simple php mysql connection and query the data we need. More about php mysql data connection http://www.bKjia.c0m/phper/18/60db56d779d2a21cfc4c596e1c3880e4.htm http://www.bkjia.com/PHPjc/632938.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632938.htmlTechArticleThis is an article using the mysql_connect function of php to connect to the mysql database. The prompt condition is that your php has php_mysql.dll. And;extension=php_mysql in libmysql.dll and php.ini, remove the preceding "...
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