这是一篇利用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 'GBK'"); //使用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连接并查询出我们需要的数据了.
文章地址:
转载随意^^请带上本文地址!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download
The most popular open source editor

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
Visual web development tools