當前位置:首頁 » 數據倉庫 » php搜索資料庫代碼
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

php搜索資料庫代碼

發布時間: 2022-08-31 21:58:30

Ⅰ 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'];
}