使用mysql函数mysqli函数连接 *** 作数据可即可,或者使用PDO
使用mysqli步骤:
1. 连接MySQL数据库
2. 判断是否连接成功
3. 选择数据库
(前三步可简写成:$link = @mysqli_connect('localhost', 'root', '', 'lx') or exit('数据库连接失败'))
4. 设置字符集
5. 准备SQL语句
6. 向MySQL服务发送SQL语句
7. 解析处理结果集
8. 释放结果集,关闭数据库连接
案例:
<?phpheader('Content-type:text/htmlcharset=utf-8')
//1.连接数据库服务器 mysqli mysql -u root -p
$link = @mysqli_connect('localhost','root','')
//var_dump($link)
//2.判断连接是否成功 信息提示GBK编码
if(mysqli_connect_errno()){
exit('数据库连接失败原因:'.mysqli_connect_error())
}
//3.选择数据库 连接标识 数据库名称
if(!mysqli_select_db($link, 'wz')){
exit('数据库选择失败')
}
//4.设置字符集
mysqli_set_charset($link, 'utf8')
//5.准备SQL
$username = 'zhangsan'
$password = md5('12345')
$pic = '32545.jpg'
$sql = "insert into user2(uname,password,pic) values('{$username}','{$password}','{$pic}')"
/* echo $sql
exit */
//6.执行SQL
$res = mysqli_query($link, $sql)
//7.判断执行结果
if($res){
//成功
echo '成功'
}else{
//失败
echo '失败'
}
//8.关闭数据库连接
mysqli_close($link)
html只能通过ajax接口跟后台数据库接口通信,然后取出来的数据在页面上显示。以下是一个例子:
html文件suggest.html:
<html>
<head>
<meta http-equiv="content-type" content="text/htmlcharset=UTF-8">
<script language="JavaScript" type="text/javascript" src="ajax_search.js"></script>
<style type="text/css" media="screen">
body {
font: 11px arial
}
.suggest_link {
background-color: #FFFFFF
padding: 2px 6px 2px 6px
cursor:hand
}
.suggest_link_over {
background-color: #E8F2FE
padding: 2px 6px 2px 6px
}
#search_suggest {
position:relative
background-color: #FFFFFF
text-align: left
border: 1px solid #000000
left: -12px
top: 1px
height: 74px
width: 170px
display:none
}
</style>
</head>
<body background="bg.gif">
<center>
<h3>实现搜索提示</h3>
<div style="width: 500px">
<input type="text" id="txtSearch" name="txtSearch" onKeyUp="searchSuggest()" autocomplete="off" />
<input type="submit" id="cmdSearch" name="cmdSearch" value="搜索"/><br />
<div id="search_suggest"></div>
</div>
</center>
</body>
</html>
ajax_search.js:
var searchReq = createAjaxObj()
function createAjaxObj()
{
var httprequest=false
if (window.XMLHttpRequest) //为非IE浏览器生成XmlHttpRequest对象
{
httprequest=new XMLHttpRequest()
if (httprequest.overrideMimeType) httprequest.overrideMimeType('text/xml')
}
else if (window.ActiveXObject) //为IE浏览器生成XmlHttpRequest对象
{
try
{
httprequest=new ActiveXObject("Msxml2.XMLHTTP")
}
catch (e)
{
try
{
httprequest=new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
return httprequest
}
function searchSuggest()
{
if (searchReq.readyState == 4 || searchReq.readyState == 0)
{
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)