search
Homephp教程php手册php 连接mysql实例代码

这是一篇利用php的mysql_connect函数来实现连接mysql数据库,前提条件是要将php.ini中的;extension=php_mysql,去掉前面的“;”.代码如下:

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

连接再选择数据库,代码如下:

<?php @mysql_select_db("test") //选择数据库mydb ?>

这样我们就可以实现连接到数据库了,可能碰到的问题,运行代码出现:Call  to  undefined  function  'mysql_connect()'… 失败,这个提示function undefined是说没有mysql_connect()函数.

解决方法如下:

将php_mysql.dll和libmysql.dll文件拷贝至c:winntsystem32中(我漏了libmysql.dll),找到php.ini中的;extension=php_mysql,去掉前面的";"  重启服务器.

好了万事具备了,现在我们就差一个简单的连接实例,例1,conn.php文件代码如下:

<?php 
$conn = @mysql_connect("localhost", "root", "root") or die("数据库链接错误");//开源代码phpfensi.com 
mysql_select_db("data", $conn);//data为数据库名称 
mysql_query("set names &#39;GBK&#39;"); //使用GBK中文编码; 
?>

index.php文件代码如下:

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

这样我们就实现的简单的php mysql连接并查询出我们需要的数据了.


文章地址:

转载随意^^请带上本文地址!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version