php查詢數(shù)據(jù)表信息代碼 php查詢數(shù)據(jù)庫并輸出實例

PHP查詢代碼怎么寫?

連接數(shù)據(jù)庫

成都創(chuàng)新互聯(lián)公司專業(yè)網(wǎng)站建設,網(wǎng)站制作與網(wǎng)站建設公司,1800元做網(wǎng)站建設全包,免費贈送網(wǎng)站基礎優(yōu)化服務,讓你的網(wǎng)站變得更有價值,公司擁有完善的專業(yè)網(wǎng)站設計公司流程,能夠為企業(yè)提供建站服務。使用PHP+MYSQL開發(fā)可交付網(wǎng)站源代碼;符合網(wǎng)站優(yōu)化排名的后臺管理系統(tǒng);網(wǎng)站制作收費合理;免費進行網(wǎng)站備案等企業(yè)網(wǎng)站建設一條龍服務.

代碼如下

?php

$conn = @mysql_connect("localhost","root","root") or die ("database error");

mysql_select_db("DB",$conn);

if (isset($_POST['submit'])){

$num=$_POST['num'];

$sql="SELECT num FROM TEST WHERE num=$num";

$tt=mysql_query($sql,$conn);

$row = mysql_fetch_assoc($tt);

echo "num:".$row['num']."/br";

}

?

PHP中寫一個數(shù)據(jù)庫查詢的類的方法代碼要如何寫

?php

if(!defined("INCLUDE_MYSQL_OK")) {

define("INCLUDE_MYSQL_OK","");

class MySQL_class {

var $debug = true;

var $db,

$id,

$result, /* 查詢結果指針 */

$rows, /* 查詢結果行數(shù) */

$fields, /* 查詢結果列數(shù) */

$data, /* 數(shù)據(jù)結果 */

$arows, /* 發(fā)生作用的紀錄行數(shù)目 */

$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); /* 定位到指定數(shù)據(jù)庫 */

$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 ("不能執(zhí)行查詢(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 ("不能執(zhí)行查詢(query): $query");

else

return false;

}

$this-result = $ret;

$this-rows = @mysql_num_rows($this-result);

$this-fields = @mysql_num_fields($this-result);

return true;

}

# 如果查詢結果為單條記錄時使用,返回結果存儲于數(shù)組 data 中

function QueryRow ($query) {

$this-result = @mysql_query($query, $this-id);

if (!$this-result)

{

if ($this-debug)

MySQL_ErrorMsg ("不能執(zhí)行查詢(query): $query");

else

return false;

}

$this-rows = @mysql_num_rows($this-result);

$this-data = @mysql_fetch_array($this-result, MYSQL_ASSOC);

//MySQL_ErrorMsg ("不能從查詢結果中取得數(shù)據(jù) $query");

if (!$this-result || !$this-rows)

return false;

return true;

}

# 移動到指定記錄行,將該行結果儲存于數(shù)組 data 中

function Fetch ($row) {

if(!@mysql_data_seek($this-result, $row))

//MySQL_ErrorMsg ("不能定位到指定數(shù)據(jù)行 $row");

return false;

$this-data = @mysql_fetch_array($this-result, MYSQL_ASSOC);

//MySQL_ErrorMsg ("不能提取指定數(shù)據(jù)行數(shù)據(jù) $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 ("不能執(zhí)行查詢(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 ("不能執(zhí)行查詢(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 ("不能執(zhí)行查詢(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系統(tǒng)提示:".$msg."br";

$text .= "錯誤信息:";

$text .= mysql_error()."br";

$text .= "錯誤代碼:".mysql_errno()."brbr";

$text .= "請稍候再試,如果問題仍然存在,請與 a href=\"mailto:wuqiong@igenus.org\"系統(tǒng)管理員/a 聯(lián)系!";

$text .= "/font\n";

die($text);

}

}

?

一些細節(jié)的地方自己修改吧 主要是我在別的文件專門定義了全局變量,你看一遍,把應改的地方改一下就好了

用PHP代碼如何查詢數(shù)據(jù)庫表中的一條記錄

我直接在這給你修改答案算了

使用的時候刪除行號 修改數(shù)據(jù)庫配置 如果想使用 頁面不刷新查詢數(shù)據(jù)庫 需要使用JQUERY 如果有需要給我留言

1 ?php

2 if(isset($_POST['submit'])$_POST['submit']=='提交'){

3 //判斷是否是提交過來的

4 $intext = $_POST['intext'];

5 if($intext!=null||$intext!=''){

6 $link = mysql_connect("localhost", "root", "123456");

7 //數(shù)據(jù)庫配置信息 第一個參數(shù)數(shù)據(jù)庫位置第二個是用戶名第三個是密碼

8 mysql_select_db("szn_test");

9 //設置要使用的數(shù)據(jù)庫

10 $sql = "select * from demo where res = '".$intext."'";

11 //SQL語句

12 var_dump($sql);

13 $res = mysql_query($sql);

14 $arr = array();

15 //吧結果存入數(shù)組 并記錄數(shù)組長度

16 $count = 0;

17 while($data = mysql_fetch_array($res)){

18 $arr[$count] = $data;

19 $count++;

20 }

21 //關閉數(shù)據(jù)庫

22 mysql_close($link);

23 }

24 }

25

26 ?

27 html

28 head

29 title/title

30 /head

31 body

32 form id="form1" method="post" action="demo.php"

33 input type="text" name="intext"

34 input type="submit" name="submit" value="提交"

35 /form

36 ?php

37 if(isset($arr)$arr != null){

38 for($i = 0; $i $count; $i++){

39 foreach($arr[$i] as $key = $value){

40 echo "key:".$key." value:".$value;

41 echo " ";

42 }

43 echo "br";

44 }

45 }

46 ?

47 /body

48 /html

這個是數(shù)據(jù)庫查詢代碼 你可以看以下對照著修改修改

PHP數(shù)據(jù)庫查詢代碼

php

變量

的話,要用數(shù)據(jù)庫連接符,放在

字符串

里不會被轉(zhuǎn)成值。

$sql

=

"

select

*

from

g4_board_file

where

bo_table

=

'$bo_table'

and

wr_id

=

'".$view[wr_id]'."'

order

by

bf_no";

把變量單獨拿出來,再把字符串連起來。

如何用php獲取數(shù)據(jù)庫信息并顯示

獲取ppq數(shù)據(jù)庫的所有表名的代碼:

?php

$server='localhost';

$user='root';

$pass='12345';

$dbname='ppq';

$conn=mysql_connect($server,$user,$pass);

if(!$conn)

die("數(shù)據(jù)庫系統(tǒng)連接失??!");

$result=mysql_list_tables($dbname);

if(!$result)

die("數(shù)據(jù)庫連接失敗!");

while($row=mysql_fetch_row($result))

{

echo

$row[0]."

";

}

mysql_free_result($result);

?

mysql_list_tables

(PHP

3,

PHP

4

,

PHP

5)

mysql_list_tables

--

列出

MySQL

數(shù)據(jù)庫中的表

說明

resource

mysql_list_tables

(

string

database

[,

resource

link_identifier])

mysql_list_tables()

接受一個數(shù)據(jù)庫名并返回和

mysql_query()

函數(shù)很相似的一個結果指針。用

mysql_fetch_array()或者用mysql_fetch_row()來獲得一個數(shù)組,數(shù)組的第0列就是數(shù)組名,當獲取不到時

mysql_fetch_array()或者用mysql_fetch_row()返回

FALSE。

分享文章:php查詢數(shù)據(jù)表信息代碼 php查詢數(shù)據(jù)庫并輸出實例
地址分享:http://muchs.cn/article26/dosspcg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、營銷型網(wǎng)站建設、品牌網(wǎng)站建設、建站公司、網(wǎng)站改版網(wǎng)站維護

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)