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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools