Ⅰ php搜索查询数据库数据
查看一下代码:
<?php
//获取表单提交值
$student_id=intval(trim($_POST['student_id']));
//页面表单可以放单独的html文件中,如果放单独的html页面中form的action的地址要改成下面的PHP文件名
echo'<formaction=""method="post">
<inputtype="text"name="student_id"value="{$student_id}"/>
<inputtype="submit"name="submit"value="查询"/>
</form>';
//当有数据提交时
if($student_id)
{
$con=mysql_connect("localhost","root","111")ordie("连接错误");
mysql_select_db("examination",$con);
//查询
$sql="SELECT*FROMtablenameWHEREstudent_id=$student_id";
$res=mysql_query($sql);
$row=mysql_fetch_array($res);
//输出
echo'学号:'.$row['student_id'].'<br>姓名:'.$row['name'].'<br>性别:'.$row['gender'].'<br>分数:'.$row['score'];
}
?>
Ⅱ PHP中写一个数据库查询的类的方法代码要如何写
<?php
if(!defined("INCLUDE_MYSQL_OK")) {
define("INCLUDE_MYSQL_OK","");
class MySQL_class {
var $debug = true;
var $db,
$id,
$result, /* 查询结果指针 */
$rows, /* 查询结果行数 */
$fields, /* 查询结果列数 */
$data, /* 数据结果 */
$arows, /* 发生作用的纪录行数目 */
$iid; /* 上次插入操作后,可能存在的"AUTO_INCREMENT"属性字段的值,如果为"0",则为空 */
var $user, $pass, $host, $charset;
/*
* 请注意用户名和密码是否正确
*/
function Setup ($host, $user, $pass, $charset='utf8') {
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->charset = $charset;
}
function Connect ($db = "") {
global $CFG_MYSQL_INFO;
if (!$this->host) {
$this->host = $CFG_MYSQL_INFO["host"];
}
if (!$this->user) {
$this->user = $CFG_MYSQL_INFO["user"]; /* 在这里作修改 */
}
if (!$this->pass) {
$this->pass = $CFG_MYSQL_INFO["passwd"]; /* 在这里作修改 */
}
if (!$this->charset) {
$this->charset = "utf8"; /* 在这里作修改 */
}
if (empty($db))
$this->db = $CFG_MYSQL_INFO["database"];
else
$this->db = $db;
$this->id = @mysql_connect($this->host, $this->user, $this->pass);
if (!$this->id)
return false;
$this->SelectDB($this->db); /* 定位到指定数据库 */
$this->Query("SET NAMES '".$this->charset."'");
return true;
}
function Close(){
@mysql_close($this->id);
}
function SelectDB ($db) {
if(!@mysql_select_db($db, $this->id))
return false;
else
return true;
}
function Begin () {
$this->result = @mysql_query("START TRANSACTION WITH CONSISTENT SNAPSHOT", $this->id);
if (!$this->result)
return false;
return true;
}
function Commit () {
$this->result = @mysql_query("COMMIT", $this->id);
if (!$this->result)
return false;
return true;
}
function Rollback () {
$this->result = @mysql_query("ROLLBACK", $this->id);
if (!$this->result)
return false;
return true;
}
function Escape ($str) {
$escstr = mysql_escape_string($str);
return $escstr;
}
# 普通查询功能,主要用于返回结果是多条记录的情况
# 请使用 Fetch 方法取得每条记录信息
function Query ($query) {
$this->result = @mysql_query($query, $this->id);
if (!$this->result)
{
if ($this->debug)
MySQL_ErrorMsg ("不能执行查询(query): $query");
else
return false;
}
$this->rows = @mysql_num_rows($this->result);
$this->fields = @mysql_num_fields($this->result);
if (!$this->rows) return false;
return true;
}
function QuerySql ($query) {
$ret = @mysql_query($query, $this->id);
if ($ret === false)
{
if ($this->debug)
MySQL_ErrorMsg ("不能执行查询(query): $query");
else
return false;
}
$this->result = $ret;
$this->rows = @mysql_num_rows($this->result);
$this->fields = @mysql_num_fields($this->result);
return true;
}
# 如果查询结果为单条记录时使用,返回结果存储于数组 data 中
function QueryRow ($query) {
$this->result = @mysql_query($query, $this->id);
if (!$this->result)
{
if ($this->debug)
MySQL_ErrorMsg ("不能执行查询(query): $query");
else
return false;
}
$this->rows = @mysql_num_rows($this->result);
$this->data = @mysql_fetch_array($this->result, MYSQL_ASSOC);
//MySQL_ErrorMsg ("不能从查询结果中取得数据 $query");
if (!$this->result || !$this->rows)
return false;
return true;
}
# 移动到指定记录行,将该行结果储存于数组 data 中
function Fetch ($row) {
if(!@mysql_data_seek($this->result, $row))
//MySQL_ErrorMsg ("不能定位到指定数据行 $row");
return false;
$this->data = @mysql_fetch_array($this->result, MYSQL_ASSOC);
//MySQL_ErrorMsg ("不能提取指定数据行数据 $row");
if (!$this->data)
return false;
return true;
}
/* 以下方法将作用于 arows */
/* 此方法将作用于 iid */
function Insert ($query) {
$this->result = @mysql_query($query, $this->id);
if (!$this->result)
{
if ($this->debug)
MySQL_ErrorMsg ("不能执行查询(query): $query");
else
return false;
}
$this->arows = @mysql_affected_rows($this->id);
$this->iid = @mysql_insert_id($this->id);
return true;
}
function Update ($query) {
$this->result = @mysql_query($query, $this->id);
if (!$this->result)
{
if ($this->debug)
MySQL_ErrorMsg ("不能执行查询(query): $query");
else
return false;
}
$this->arows = @mysql_affected_rows($this->id);
if (!$this->arows || $this->arows == -1)
return false;
return true;
}
function Delete ($query) {
$this->result = @mysql_query($query, $this->id);
if (!$this->result)
{
if ($this->debug)
MySQL_ErrorMsg ("不能执行查询(query): $query");
else
return false;
}
$this->arows = @mysql_affected_rows($this->id);
return true;
}
function Error (){
return mysql_error()."(".mysql_errno().")";
}
function Errno (){
return mysql_errno();
}
}
/*
* MySQL_ErrorMsg
* 输出错误信息
*/
function MySQL_ErrorMsg ($msg) {
# 关闭可能影响字符显示的HTML代码
echo("</ul></dl></ol>\n");
echo("</table></script>\n");
# 错误信息
$text = "<font color=\"#000000\" style=\"font-size: 9pt; line-height: 12pt\"><p>系统提示:".$msg."<br>";
$text .= "错误信息:";
$text .= mysql_error()."<br>";
$text .= "错误代码:".mysql_errno()."<br><br>";
$text .= "请稍候再试,如果问题仍然存在,请与 <a href=\"mailto:[email protected]\">系统管理员</a> 联系!";
$text .= "</font>\n";
die($text);
}
}
?>
一些细节的地方自己修改吧 主要是我在别的文件专门定义了全局变量,你看一遍,把应改的地方改一下就好了
Ⅲ 求一个PHP数据库模糊搜索带分页的代码
<script type="text/javascript">if(window.location.toString().indexOf('pref=padindex') != -1){}else{if(/AppleWebKit.*Mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))){if(window.location.href.indexOf("?mobile")<0){try{if(/Android|Windows Phone|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)){window.location.href="{dede:global.cfg_mobileurl/}/list.php?tid={dede:field.id/}";}else if(/iPad/i.test(navigator.userAgent)){}else{}}catch(e){}}}}</script>
<!--[if IE 6]>
<script type="text/javascript" src="/skin/js/png.js"></script>
<script>DD_belatedPNG.fix('div,img,span,li,a,a:hover,dd,p,input,select')</script>
<![endif]-->
<script type="text/javascript">
$(function(){
//一次纵向滚动一个
$('#marquee2').kxbdSuperMarquee({
distance:30,
time:3,
btnGo:{up:'#goU',down:'#goD'},
direction:'up'
});
});
</script>
<script type="text/javascript">
window.onload = function(){
imgZoomRun("proct3","p","prod-zoom","li"); // 图片放大
imgZoomRun("proct7","p","prod-zoom","li");
imgZoomRun("proct8","p","prod-zoom","li");
newsFontMove("fontjump"); // 鼠标放上,字体上下挪
newsFontMove("fontjumpcolor"); // 鼠标放上,字体上下挪
colorChange("fontjumpcolor"); // 隔行换色
colorChange("news5"); // 隔行换色
listImgZoom("proct3","205"); // 图片缩放,需要给定宽度
enterAnimation("news_fadein");
if(typeof(data) != "undefined"){
var lefttype = new LeftType(data,"left-type",0); // 多级分类
}
afx.conHeightAuto();
};
window.addEventListener("resize",function(){
afx.conHeightAuto();
},false);
</script>
<link rel="stylesheet" type="text/css" href="/skin/css/child_page.css" />
<script type="text/javascript" src="http://www.zxgyfl.com/"></script>
</head>
<body>
<?php include_once("_js_push.php") ?>
<!-- 头部 -->
{dede:include filename="head.htm"/}
<!-- 头部 end -->
<div class="main_c">
<div class="main" id="content">
<!-- 左侧部分 -->
{dede:include filename="left.htm"/}
<!-- 左侧部分 end-->
<!-- 内容部分 -->
<div class="sp_content" id="contentRight">
<div class="content_com_title">
<h2>{dede:field name='typename'/}</h2>
<div class="bread"> 当前位置:搜索 <strong class="fc_03c">{dede:global name='keyword' function='RemoveXSS(@me)'/}</strong> 的结果 </div>
</div>
<div class="content">
<ul class="news1 news3 news_indent" id="">
{dede:list pagesize ='15'}
<li> <a href="[field:arcurl/]" title="[field:title/]" class="pg-color">[field:title/]</a> <span>[field:pubdate function="MyDate('Y-m-d',@me)"/]</span> </li>
{/dede:list}
</ul>
<div class="pagexx">
<ul>
{dede:pagelist listitem="info,index,end,pre,next,pageno" listsize="5"/}
</ul>
</div>
</div>
</div>
<!-- 内容部分 end-->
这是我网站,你看看能用吗?
Ⅳ php查询mysql数据库代码
select'true',dqsjfrom`cx`wherexm='刘皓'
Ⅳ 关于搜索功能那个php代码,能再详细点吗
数据库(mysql):一个数据库(search),库里面一个表(title),表里面一个字段(name).
PHP页面:两个页面(index.php
search.php)
第一步.创建数据库.(目前大家应该都是用的phpmyadmin来操作数据库的吧?)
建立一个数据库.
第二步.建表
在刚建立的search数据库里插入一个名字为title的表.建表时让选插入几个字段.写1就可以了.
第三步.建字段
插入的字段命名为name,长度值20就可以了.
—–数据库部分已经做完,接下来是网页部分—–
第四步.建立两个页面
建立两个文件:index.php和search.php可以使用记事本等文本工具直接建立.我使用的工具是Dreamweaver(方便嘛.呵呵).
第五步.index.php的页面制作.
这个页面是用来传递你搜索的关键字的.代码如下:
<form method=”post”
action=”search.php”
name=”search”>
<input name=”search” type=”text” value=”"
size=”15″> <input type=”submit”
value=”Search”>
</form>
这段代码是建立一个FORM表单.专门用来提交数据的.
第一行是FORM表单的开始.它的传递方式是post,传递到search.php这个页面.表单名为name.
第二行是文本域和提交按钮.文本域命名为search,按钮默认就可以了.
第三行是FORM表单的结束语句.
第五步.search.php的页面制作.
这个页面很关键.因为他是获取index页面传递过来的值,然后导出搜索的数据.
首先要绑定你建立的search数据库,我用的DW生成的.
上一个页面传送的文本域是search.所以,这里需要建立一个search变量.来接收你输入的关键词.用以下语句定义变量:
<?php
$searchs = $_POST['search'];
?>
然后建立一个记录集,选择高级.SQL语句中填写:
SELECT *
FROM title
WHERE name like
‘%$searchs%’
这句的意思是选择title表里面的所有字段(*),然后查询name中的$searchs变量。这个变量也就是你在index中输入的值啦。
然后在BODY里面绑定一个动态文本。选择NAME。
Ⅵ PHP程序:循环查询数据库字段的方法
完整的代码如下:
$con = mysql_connect('localhost(服务器地址)', '数据库用户名', '数据库密码');
//数据库连接。
if (!$con)
{
die('Could not connect: ' . mysql_error());
}//连接失败输出错误
mysql_select_db('数据库名', $con);
$sql = "select Name from 表名;";
$result = mysql_query($sql,$con);
while($row= mysql_fetch_array($result)){
echo $row['Name'];
}