本文目錄一覽:
- 1、php數據庫內容修改代碼
- 2、求PHP數據庫封裝類操作代碼
- 3、PHP的POST方法和操作數據庫的代碼
- 4、PHP數據庫查詢代碼
- 5、幾種常用PHP連接數據庫的代碼示例
- 6、php中和後台數據庫連接的代碼
php數據庫內容修改代碼
修改如下:不用使用session傳遞
1.php 文件中: 修改後的代碼,將$row[“id”]作為id的參數值傳遞到2.php
else{
echo ‘td’.’a href=”2.php?id=’.$row[“id”].'”‘.$row[“id”].可以修改.’/a/td’;
}
2.php修改如下:
$strSql=”SELECT * from test where id=”.$_GET[‘id’];
求PHP數據庫封裝類操作代碼
?php
class MySQL{
private $host; //服務器地址
private $name; //登錄賬號
private $pwd; //登錄密碼
private $dBase; //數據庫名稱
private $conn; //數據庫鏈接資源
private $result; //結果集
private $msg; //返回結果
private $fields; //返回字段
private $fieldsNum; //返回字段數
private $rowsNum; //返回結果數
private $rowsRst; //返回單條記錄的字段數組
private $filesArray = array(); //返回字段數組
private $rowsArray = array(); //返回結果數組
private $charset=’utf8′; //設置操作的字符集
private $query_count=0; //查詢結果次數
static private $_instance; //存儲對象
//初始化類
private function __construct($host=”,$name=”,$pwd=”,$dBase=”){
if($host != ”) $this-host = $host;
if($name != ”) $this-name = $name;
if($pwd != ”) $this-pwd = $pwd;
if($dBase != ”) $this-dBase = $dBase;
$this-init_conn();
}
//防止被克隆
private function __clone(){}
public static function getInstance($host=”,$name=”,$pwd=”,$dBase=”){
if(FALSE == (self::$_instance instanceof self)){
self::$_instance = new self($host,$name,$pwd,$dBase);
}
return self::$_instance;
}
public function __set($name,$value){
$this-$name=$value;
}
public function __get($name){
return $this-$name;
}
//鏈接數據庫
function init_conn(){
$this-conn=@mysql_connect($this-host,$this-name,$this-pwd) or die(‘connect db fail !’);
@mysql_select_db($this-dBase,$this-conn) or die(‘select db fail !’);
mysql_query(“set names “.$this-charset);
}
//查詢結果
function mysql_query_rst($sql){
if($this-conn == ”) $this-init_conn();
$this-result = @mysql_query($sql,$this-conn);
$this-query_count++;
}
//取得字段數
function getFieldsNum($sql){
$this-mysql_query_rst($sql);
$this-fieldsNum = @mysql_num_fields($this-result);
}
//取得查詢結果數
function getRowsNum($sql){
$this-mysql_query_rst($sql);
if(mysql_errno() == 0){
return @mysql_num_rows($this-result);
}else{
return ”;
}
}
//取得記錄數組(單條記錄)
function getRowsRst($sql,$type=MYSQL_BOTH){
$this-mysql_query_rst($sql);
if(empty($this-result)) return ”;
if(mysql_error() == 0){
$this-rowsRst = mysql_fetch_array($this-result,$type);
return $this-rowsRst;
}else{
return ”;
}
}
//取得記錄數組(多條記錄)
function getRowsArray($sql,$type=MYSQL_BOTH){
!empty($this-rowsArray) ? $this-rowsArray=array() : ”;
$this-mysql_query_rst($sql);
if(mysql_errno() == 0){
while($row = mysql_fetch_array($this-result,$type)) {
$this-rowsArray[] = $row;
}
return $this-rowsArray;
}else{
return ”;
}
}
//更新、刪除、添加記錄數
function uidRst($sql){
if($this-conn == ”){
$this-init_conn();
}
@mysql_query($sql);
$this-rowsNum = @mysql_affected_rows();
if(mysql_errno() == 0){
return $this-rowsNum;
}else{
return ”;
}
}
//返回最近插入的一條數據庫的id值
function returnRstId($sql){
if($this-conn == ”){
$this-init_conn();
}
@mysql_query($sql);
if(mysql_errno() == 0){
return mysql_insert_id();
}else{
return ”;
}
}
//獲取對應的字段值
function getFields($sql,$fields){
$this-mysql_query_rst($sql);
if(mysql_errno() == 0){
if(mysql_num_rows($this-result) 0){
$tmpfld = @mysql_fetch_row($this-result);
$this-fields = $tmpfld[$fields];
}
return $this-fields;
}else{
return ”;
}
}
//錯誤信息
function msg_error(){
if(mysql_errno() != 0) {
$this-msg = mysql_error();
}
return $this-msg;
}
//釋放結果集
function close_rst(){
mysql_free_result($this-result);
$this-msg = ”;
$this-fieldsNum = 0;
$this-rowsNum = 0;
$this-filesArray = ”;
$this-rowsArray = ”;
}
//關閉數據庫
function close_conn(){
$this-close_rst();
mysql_close($this-conn);
$this-conn = ”;
}
//取得數據庫版本
function db_version() {
return mysql_get_server_info();
}
}
PHP的POST方法和操作數據庫的代碼
?
$db_host = “localhost”;//鏈接的數據庫地址,也就是主機名字
$db_user = “db”;//數據庫名字
$db_pass = “數據庫密碼”;
$db_name = “msg”;//表名
$connec = mysql_connect($db_host,$db_user,$db_pass) or die(“不能連接數據庫服務器: “.mysql_error());
mysql_select_db($db_name,$connec) or die (“不能選擇數據庫: “.mysql_error());
$user=$_POST[‘user’]; //$_post不用大寫的就沒用得
$sms=$_POST[‘sms’];
$ID=$_POST[‘id’];
$db_query=’INSERT INTO msg(表名) VALUES $user,$sms,$ID’;//插入
mysql db query($db_query);//運行sql語句
?
上面的程序改改就可以用了,或許有問題,我在網吧,沒調試的!
我也是學PHP的,現在還很菜,有時間的話咱交流交流!
PHP數據庫查詢代碼
php變量的話,要用數據庫連接符,放在字符串里不會被轉成值。
$sql = ” select * from g4_board_file where bo_table = ‘$bo_table’ and wr_id = ‘”.$view[wr_id]’.”‘ order by bf_no”;
把變量單獨拿出來,再把字符串連起來。
幾種常用PHP連接數據庫的代碼示例
sybase_connect連上數據庫。
語法: int sybase_connect(string [servername], string [username], string [password]);
返回值: 整數函數種類: 數據庫功能 本函數用來打開與 Sybase 數據庫的連接。
參數 servername 為欲連上的數據庫服務器名稱。
參數 username 及 password 可省略,分別為連接使用的帳號及密碼。
使用本函數需注意早點關閉數據庫,以減少系統的負擔。
連接成功則返回數據庫的連接代號,失敗返回 false 值。
php中和後台數據庫連接的代碼
?php
mysql_connect(“localhost”,”你的名字,一般為root”,”你的密碼”)or
die(“cannot
connect
with
the
localhost.”);
mysql_slect_db(“你的數據庫名字”)
or
die(“cannot
connect
with
the
database.”);
//這就是連接數據庫的代碼,簡單的寫法。
?
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/285265.html