孫策
算了給你這個(gè),我在PHP官方網(wǎng)站上找的例子修改了一下:顯示完整數(shù)據(jù)表----------<?php// 連接,選擇數(shù)據(jù)庫$link = ("數(shù)據(jù)庫鏈接","數(shù)據(jù)庫用戶名","密碼") or die('數(shù)據(jù)庫死翹翹了:' . mysql_error());echo 'Connected successfully';mysql_select_db('這里確實(shí)是填數(shù)據(jù)庫名') or die('數(shù)據(jù)表壞掉了,快打120吧...');// 執(zhí)行 SQL 查詢$query = 'SELECT * FROM 數(shù)據(jù)表';$result = mysql_query($query) or die('查詢錯(cuò)誤:' . mysql_error());// 用 HTML 顯示結(jié)果echo "<table>\n";while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "\t<tr>\n"; foreach ($line as $col_value) { echo "\t\t<td>$col_value</td>\n"; } echo "\t</tr>\n";}echo "</table>\n";// 釋放結(jié)果集mysql_free_result($result);// 關(guān)閉連接mysql_close($link);?>----------ps:你是初學(xué)PHP吧~~平時(shí)多看www.php.net上的手冊吧,最好的導(dǎo)師就是手冊~!
參考資料:http://cn2.php.net/manual/zh/ref.mysql.php
葉正芳
$connection = @mysql_connect( '127.0.0.1:3306', 'root', '111111', true ); if( !$connection ) { die('連接數(shù)據(jù)庫失敗'); } // dbname 你的數(shù)據(jù)庫名字 mysql_select_db( 'dbname', $connection ) or die('數(shù)據(jù)庫不存在'); // sql 語句 $sqlString = '你的sql語句 select[delete][insert][update]...'; $result = mysql_query( $sqlString, $connection ); // 只有delete insert update 語句使用以下代碼 $result = mysql_affected_rows( $connection ); return $result ? '操作成功' : '操作失敗'; // 只有select需要使用以下代碼 // 獲取一行數(shù)據(jù) $rowArray = mysql_fetch_assoc( $result ); // 獲取多行數(shù)據(jù)請用 /* while( $row = mysql_fetch_assoc( $result ) ) { $resultArray[] = $row; } */ // 打印結(jié)果集 var_dump($rowArray); // 取其中某個(gè)字段 fieldname mysql字段名 // echo $rowArray['fieldname'];
朱砂吼
你連接Sql文的時(shí)候,連接的變量中有值為Null的變量存在。
陸抗
thinkphp聯(lián)鏈接數(shù)據(jù)庫在
/common/conf/congif.php里設(shè)置相應(yīng)的參數(shù)
tp3.2如下設(shè)置
對數(shù)據(jù)庫操作如下:
public function test(){ //查詢單條數(shù)據(jù) $result=M("表名")->where("條件")->find(); //查詢多條數(shù)據(jù) $result=M("表名")->where("條件")->find(); //刪除數(shù)據(jù) $result=M("表名")->where("條件")->del(); //修改數(shù)數(shù)據(jù) $data['字段名稱']="值"; $result=M("表名")->where("條件")->save($data); //新增數(shù)據(jù) $data['字段名稱']="值"; $result=M("表名")->add($data); }tp5數(shù)據(jù)庫配置
tp5數(shù)據(jù)庫配置application/datebase.php
火無害
//先創(chuàng)建一個(gè)log表,有id, action, username, query, time 等字段,如果需要記錄,可添加$query_string = $_SERVER['QUERY_STRING'];//查詢(query)的字符串,這個(gè)最好處理一下$action = $_REQUEST['action'];//操作類型,可以自己定義。比如?action=add&id=xx//$username就是當(dāng)前操作人的名字了,登錄后都有的吧if(in_array($action, array('add', 'edit','delete','update'))){ addlog($action,$username,$query_string);}function addlog($action,$username,$query_string){ //這里可以把時(shí)間和$query_string處理一下,插入數(shù)據(jù)庫 $db->query($sql);}大致思路就是這樣的了,細(xì)節(jié)還需你自己完善
魔經(jīng)
是你代碼問題,你的SQL語句寫錯(cuò)了。其實(shí)與你上面在phpmyadmin中執(zhí)行的語句是一樣的。$sql="INSERT INTO test (id,uid,regdate,remark)values(``,`php222`,now(),`工人`)";應(yīng)該是$sql="INSERT INTO test (id,uid,regdate,remark)values('','php222',now(),'工人')";注意,MYSQL語句中,` 號(Esc下面那個(gè)按鈕)與單引號使用是不同的。` :一般用來包含表名,表中的字段名。' :一般是用來包含 varchar, text, longtext 等字符型數(shù)據(jù)的。
劉敬選
<?php//獲取POST表單提交的username$username = isset($_POST['username']) ? trim($_POST['username']) : '';// 同上$password = isset($_POST['password']) ? trim($_POST['password']) : '';//使用pdo連接mysql數(shù)據(jù)庫$conn = new PDO('mysql:host=localhost;dbname=test', 'root', 'root');//使用參數(shù)綁定查詢記錄$rs = $conn->prepare("SELECT `uid` FROM `test_user` WHERE `username` = :username AND `password` <> :password");$rs->execute(array(':username' => $username,':password' => $password));if($rs->columnCount() > 0){echo '登錄成功';}else{echo '登錄失敗';}columncount是什么語法?
是PDOStatement類的一個(gè)方法,返回結(jié)果集的個(gè)數(shù)
我只學(xué)了php和HTML5啊,其他的都不知道,能只用php的語法給我說說嘛
我這代碼除了php啥都沒有,哦 有一句sql語句不到50行代碼,只用了php自帶的相關(guān)方法和類,有嘛好解釋的.
邢城
對于 Web 程序員來說,數(shù)據(jù)庫只要掌握數(shù)據(jù)庫查詢語言(SQL)和數(shù)據(jù)庫系統(tǒng)的基本操作即可。SQL 基本上都是增刪改查,當(dāng)然還有數(shù)據(jù)庫的設(shè)計(jì)(數(shù)據(jù)庫搭建),這就需要了解數(shù)據(jù)庫設(shè)計(jì)規(guī)范和數(shù)據(jù)庫系統(tǒng)中的數(shù)據(jù)結(jié)構(gòu)(或者說列類型等),其次對于一些復(fù)雜化的數(shù)據(jù)庫設(shè)計(jì)還要涉及觸發(fā)器和存儲(chǔ)過程。在實(shí)際開發(fā)過程中數(shù)據(jù)庫設(shè)計(jì)更花費(fèi)精力,不過編碼過程中無非就是 SELECT/DELETE/INSERT/UPDATE 等等。當(dāng)然除了 SQL 本身,還有在編程那部分,要了解在程序中如何使用 SQL,比如 PHP 中就有 MySQL API,要熟悉了這些語法才能把數(shù)據(jù)庫和程序連接起來,另外,有些成熟的構(gòu)件中還會(huì)使用專用語法,這需要單獨(dú)學(xué)習(xí),但暫時(shí)沒遇到就不用管它了。至于數(shù)據(jù)庫系統(tǒng)的基本操作,就是日常使用和維護(hù),一般包括就會(huì)安裝數(shù)據(jù)庫系統(tǒng)軟件、創(chuàng)建實(shí)例(數(shù)據(jù)庫)、設(shè)置連接、設(shè)置權(quán)限等,這些可以參考數(shù)據(jù)庫系統(tǒng)的支持文檔或技術(shù)手冊。現(xiàn)在大多數(shù)數(shù)據(jù)庫都提供圖形化管理界面,比如 MySQL 的 MySQL Workbench,操作起來非常方便。另外,在實(shí)際應(yīng)用中還包括優(yōu)化等過程,有時(shí)還要使用集群或緩存來提升數(shù)據(jù)庫系統(tǒng)的性能,不過這些問題在剛開始時(shí)就沒必要考慮了,其實(shí)數(shù)據(jù)庫管理應(yīng)該有專人負(fù)責(zé),也不需要程序員操心,了解即可。剛剛開始接觸數(shù)據(jù)庫可能會(huì)遇到一些麻煩,不過,最好的解決辦法就是先動(dòng)手去嘗試!如果遇到棘手的問題可以到網(wǎng)上找答案,網(wǎng)上這類的經(jīng)驗(yàn)還是非常多的。
竹山教
馬上給你 <?php /*===============================================================*/ /*文件名:Model.class.php */ /*概要: 模塊處理類,公用的數(shù)據(jù)庫的操作*/ /*作者:袁再新 */ /*作品聲明:本人屬于初學(xué)者,限于技術(shù)水平勿將此例作為商用*/ /*制作時(shí)間:2012-3-30 */ /*===============================================================*/ // class Model{ protected $pdo; protected $tabName; protected $messList; protected $fieldList; function __construct(){ try{ $this->pdo=new PDO('mysql:dbname='.DB_NAME.';host='.DB_HOST,DB_USER,DB_PSWD,array(PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION)); $this->pdo->exec('set names gbk');//解決字符編碼問題 }catch(PDOException $e){ echo '數(shù)據(jù)庫連接失?。?#39;.$e->getMessage(); exit; } }function add($postList){ $fieldList=''; $value=''; foreach($postList as $key=>$val){ if(in_array($key,$this->fieldList)){ $fieldList.=$key.','; if($key=='userPwd'){ $val=md5($val); } if(!get_magic_quotes_gpc()){ $value.="'".addslashes($val)."'".','; }else{ $value.="'".$val."',"; } } } $fieldList=rtrim($fieldList,','); $value=rtrim($value,','); try{ $sql='INSERT INTO '.$this->tabName.'('.$fieldList.')'.' values('.$value.')'; $affected=$this->pdo->exec($sql); }catch(PDOException $e){ echo '數(shù)據(jù)錄入失?。?#39;.$e->getMessage(); echo $sql; exit(); } return $affected; }function mod($postList){ $id=''; $value=''; $id=$postList['id']; unset($postList['id']); foreach($postList as $key=>$val){ if(in_array($key,$this->fieldList)){ if(!get_magic_quotes_gpc()){ if($key=='userPwd'){ $val=md5(addslashes($val)); }else{ $val=addslashes($val); } $value.=$key."='".$val."',"; }else{ $value.=$key."='".$val."',"; } } } $value=rtrim($value,','); try{ $sql='UPDATE '.$this->tabName.' SET '.$value.' WHERE id='.$id; $affected=$this->pdo->exec($sql); }catch(PDOException $e){ echo '更新失敗;'.$e->getMessage(); exit; } return $affected; } function del($id){ if(is_array($id)){ $tmp='IN ('.join(',',$id).')'; }else{ $tmp="$id"; } try{ $affected=$this->pdo->exec('DELETE FROM '.$this->tabName.' WHERE id='.$tmp); }catch(PDOException $e){ echo '數(shù)據(jù)刪除失?。?#39;.$e->getMessage(); exit(); } return $affected; }function find($id){ $fields=join(',',$this->fieldList); $sql='SELECT '.$fields.' FROM '.$this->tabName.' WHERE id='.$id; $stmt=$this->pdo->query($sql); if($stmt&&$stmt->rowCount()>0){ return $stmt->fetch(PDO::FETCH_ASSOC); }else{ //echo $stmt;作為調(diào)試用 return false; } } function total($where=''){ $sql='SELECT * FROM '.$this->tabName.' '.$where; $stmt=$this->pdo->query($sql); return $stmt->rowCount(); } function findAll($where='',$field=array(),$offset=0,$number=0,$isall=false,$order='id desc'){ if(empty($field)){ $field=join(',',$this->fieldList); }else{ $field=join(',',$field); } if($number==0){ $sql='SELECT '.$field.' FROM '.$this->tabName." ".$where.' ORDER BY '.$order; }else{ $sql='SELECT '.$field.' FROM '.$this->tabName." ".$where.' ORDER BY '.$order.' LIMIT '.$offset.','.$number; } $stmt=$this->pdo->query($sql); if($stmt&&$stmt->rowCount()>0&&$isall===false){ return $stmt->fetch(PDO::FETCH_ASSOC); }elseif($stmt&&$stmt->rowCount()>0&&$isall===true){ return $stmt->fetchAll(PDO::FETCH_ASSOC); }else{ return false; } } function getMessList(){ $message=''; if(!empty($this->messList)){ foreach($this->messList as $value){ $message.=$value.'<br>'; } } return $message; }}
這是一個(gè)類吧?其實(shí)我想要能實(shí)現(xiàn)的具體代碼,