本文目錄一覽:
PHP自定義函數時怎麼指定參數類型?
class User{
public $name;
public $password;
function __construct($name,$password){
$this-name=$name;
$this-password=$password;
}
}
//參數可以指定對象類型
function f1(User $user){
echo $user-name,”,”,$user-password;
}
//參數可以指定數組類型
function f2(array $arr){}
//參數不可以指定基本類型,下面一句會出錯
function f3(string $s){}
php中使用什麼函數定義一個自定義函數
php語言中,使用 function來聲明一個函數
如: 用function 聲明一個名稱為 get_article_info的函數,函數的參數為 $id
/**
* 獲得指定的文章的詳細信息
*
* @access private
* @param integer $id
* @return array
*/
function get_article_info($id)
{
/* 獲得文章的信息 */
$sql = “SELECT a.*, IFNULL(AVG(r.comment_rank), 0) AS comment_rank “.
“FROM ” .$GLOBALS[‘ecs’]-table(‘article’). ” AS a “.
“LEFT JOIN ” .$GLOBALS[‘ecs’]-table(‘comment’). ” AS r ON r.id_value = a.article_id AND comment_type = 1 “.
“WHERE a.is_open = 1 AND a.article_id = ‘$id’ GROUP BY a.article_id”;
$row = $GLOBALS[‘db’]-getRow($sql);
if ($row !== false)
{
$row[‘comment_rank’] = ceil($row[‘comment_rank’]); // 用戶評論級別取整
$row[‘add_time’] = local_date($GLOBALS[‘_CFG’][‘date_format’], $row[‘add_time’]); // 修正添加時間顯示
/* 作者信息如果為空,則用網站名稱替換 */
if (empty($row[‘author’]) || $row[‘author’] == ‘_SHOPHELP’)
{
$row[‘author’] = $GLOBALS[‘_CFG’][‘shop_name’];
}
}
return $row;
}
PHP自定義函數
outPut函數 默認$newName 參數為空,也就是說調用outPut時可以不傳第三個參數
那麼執行的時候就是imagejpeg($img); 如果傳了第三個參數 那麼if條件語句就判斷不通過,那麼將會執行imagejpeg($img,$newName)
imagejpeg函數有第二個參數的時候 是將img指定輸出到第二個參數所指定的文件名,
如果沒有第二個參數,默認輸出到瀏覽器
php 關於PDO自定義類里的函數 傳什麼樣的參數
目前而言,實現“數據庫抽象層”任重而道遠,使用PDO這樣的“數據庫訪問抽象層”是一個不錯的選擇。
PDO中包含三個預定義的類
PDO中包含三個預定義的類,它們分別是 PDO、PDOStatement 和 PDOException。
一、PDO
PDO-beginTransaction() — 標明回滾起始點
PDO-commit() — 標明回滾結束點,並執行SQL
PDO-__construct() — 建立一個PDO鏈接數據庫的實例
PDO-errorCode() — 獲取錯誤碼
PDO-errorInfo() — 獲取錯誤的信息
PDO-exec() — 處理一條SQL語句,並返回所影響的條目數
PDO-getAttribute() — 獲取一個“數據庫連接對象”的屬性
PDO-getAvailableDrivers() — 獲取有效的PDO驅動器名稱
PDO-lastInsertId() — 獲取寫入的最後一條數據的主鍵值
PDO-prepare() — 生成一個“查詢對象”
PDO-query() — 處理一條SQL語句,並返回一個“PDOStatement”
PDO-quote() — 為某個SQL中的字符串添加引號
PDO-rollBack() — 執行回滾
PDO-setAttribute() — 為一個“數據庫連接對象”設定屬性
二、PDOStatement
PDOStatement-bindColumn() — Bind a column to a PHP variable
PDOStatement-bindParam() — Binds a parameter to the specified variable name
PDOStatement-bindValue() — Binds a value to a parameter
PDOStatement-closeCursor() — Closes the cursor, enabling the statement to be executed again.
PDOStatement-columnCount() — Returns the number of columns in the result set
PDOStatement-errorCode() — Fetch the SQLSTATE associated with the last operation on the statement handle
PDOStatement-errorInfo() — Fetch extended error information associated with the last operation on the statement handle
PDOStatement-execute() — Executes a prepared statement
PDOStatement-fetch() — Fetches the next row from a result set
PDOStatement-fetchAll() — Returns an array containing all of the result set rows
PDOStatement-fetchColumn() — Returns a single column from the next row of a result set
PDOStatement-fetchObject() — Fetches the next row and returns it as an object.
PDOStatement-getAttribute() — Retrieve a statement attribute
PDOStatement-getColumnMeta() — Returns metadata for a column in a result set
PDOStatement-nextRowset() — Advances to the next rowset in a multi-rowset statement handle
PDOStatement-rowCount() — Returns the number of rows affected by the last SQL statement
PDOStatement-setAttribute() — Set a statement attribute
PDOStatement-setFetchMode() — Set the default fetch mode for this statement
PDO是一個“數據庫訪問抽象層”,作用是統一各種數據庫的訪問接口,與mysql和mysqli的函數庫相比,PDO讓跨數據庫的使用更具有親和力;與ADODB和MDB2相比,PDO更高效。目前而言,實現“數據庫抽象層”任重而道遠,使用PDO這樣的“數據庫訪問抽象層”是一個不錯的選擇。
PDO中包含三個預定義的類
PDO中包含三個預定義的類,它們分別是 PDO、PDOStatement 和 PDOException。
一、PDO
PDO-beginTransaction() — 標明回滾起始點
PDO-commit() — 標明回滾結束點,並執行SQL
PDO-rollBack() — 執行回滾
PDO-__construct() — 建立一個PDO鏈接數據庫的實例
PDO-errorCode() — 獲取錯誤碼
PDO-errorInfo() — 獲取錯誤的信息
PDO-exec() — 處理一條SQL語句,並返回所影響的條目數
PDO-getAttribute() — 獲取一個“數據庫連接對象”的屬性
PDO-getAvailableDrivers() — 獲取有效的PDO驅動器名稱
PDO-lastInsertId() — 獲取寫入的最後一條數據的主鍵值
PDO-prepare() — 生成一個“查詢對象”
PDO-query() — 處理一條SQL語句,並返回一個“PDOStatement”
PDO-quote() — 為某個SQL中的字符串添加引號
PDO-setAttribute() — 為一個“數據庫連接對象”設定屬性
詳解1) PDO中的數據庫連接
$dsn = ‘mysql:dbname=ent;host=127.0.0.1′;
$user = ‘root’;
$password = ‘123456′;
try {
$dbh = new PDO($dsn, $user, $password, array(PDO::ATTR_PERSISTENT = true));
$dbh-query(‘set names utf8;’);
foreach ($dbh-query(‘SELECT * from tpm_juese’) as $row) {
print_r($row);
}
} catch (PDOException $e) {
echo ‘Connection failed: ‘ . $e-getMessage();
}
許多Web應用會因為使用了向數據庫的持久連接而得到優化。持久連接不會在腳本結束時關閉,
相反它會被緩存起來並在另一個腳本通過同樣的標識請求一個連接時得以重新利用。
持久連接的緩存可以使你避免在腳本每次需要與數據庫對話時都要部署一個新的連接的資源消耗,讓你的Web應用更加快速。
上面實例中的array(PDO::ATTR_PERSISTENT = true)就是把連接類型設置為持久連接。
詳解2) PDO中的事務
PDO-beginTransaction(),PDO-commit(),PDO-rollBack()這三個方法是在支持回滾功能時一起使用的。PDO-beginTransaction()方法標明起始點,PDO-commit()方法標明回滾結束點,並執行SQL,PDO-rollBack()執行回滾。
?php
try {
$dbh = new PDO(‘mysql:host=localhost;dbname=test’, ‘root’, ”);
$dbh-query(‘set names utf8;’);
$dbh-setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh-beginTransaction();
$dbh-exec(”INSERT INTO `test`.`table` (`name` ,`age`)VALUES (‘mick’, 22);”);
$dbh-exec(”INSERT INTO `test`.`table` (`name` ,`age`)VALUES (‘lily’, 29);”);
$dbh-exec(”INSERT INTO `test`.`table` (`name` ,`age`)VALUES (‘susan’, 21);”);
$dbh-commit();
} catch (Exception $e) {
$dbh-rollBack();
echo “Failed: ” . $e-getMessage();
}
?
現在你已經通過PDO建立了連接,在部署查詢之前你必須搞明白PDO是怎樣管理事務的。如果你以前從未遇到過事務處理,(現在簡單介紹一下:)它們提供了4個主要的特性:原子性,一致性,獨立性和持久性(Atomicity, Consistency, Isolation and Durability,ACID)通俗一點講,一個事務中所有的工作在提交時,即使它是分階段執行的,也要保證安全地應用於數據庫,不被其他的連接干擾。事務工作也可以在請求發生錯誤時輕鬆地自動取消。
事務的典型運用就是通過把批量的改變“保存起來”然後立即執行。這樣就會有徹底地提高更新效率的好處。換句話說,事務可以使你的腳本更快速同時可能更健壯(要實現這個優點你仍然需要正確的使用它們)。
不幸運的是,並不是每個數據庫都支持事務,因此PDO需要在建立連接時運行在被認為是“自動提交”的模式下。自動提交模式意味着你執行的每個查詢都有它自己隱含的事務處理,無論數據庫支持事務還是因數據庫不支持而不存在事務。如果你需要一個事務,你必須使用 PDO-beginTransaction() 方法創建一個。如果底層驅動不支持事務處理,一個PDOException就會被拋出(與你的異常處理設置無關,因為這總是一個嚴重的錯誤狀態)。在一個事物中,你可以使用 PDO-commit() 或 PDO-rollBack() 結束它,這取決於事務中代碼運行是否成功。
當腳本結束時或一個連接要關閉時,如果你還有一個未處理完的事務,PDO將會自動將其回滾。這是對於腳本意外終止的情況來說是一個安全的方案——如果你沒有明確地提交事務,它將會假設發生了一些錯誤,為了你數據的安全,所以就執行回滾了。
二、PDOStatement
PDOStatement-bindColumn() — Bind a column to a PHP variable
PDOStatement-bindParam() — Binds a parameter to the specified variable name
PDOStatement-bindValue() — Binds a value to a parameter
PDOStatement-closeCursor() — Closes the cursor, enabling the statement to be executed again.
PDOStatement-columnCount() — Returns the number of columns in the result set
PDOStatement-errorCode() — Fetch the SQLSTATE associated with the last operation on the statement handle
PDOStatement-errorInfo() — Fetch extended error information associated with the last operation on the statement handle
PDOStatement-execute() — Executes a prepared statement
PDOStatement-fetch() — Fetches the next row from a result set
PDOStatement-fetchAll() — Returns an array containing all of the result set rows
PDOStatement-fetchColumn() — Returns a single column from the next row of a result set
PDOStatement-fetchObject() — Fetches the next row and returns it as an object.
PDOStatement-getAttribute() — Retrieve a statement attribute
PDOStatement-getColumnMeta() — Returns metadata for a column in a result set
PDOStatement-nextRowset() — Advances to the next rowset in a multi-rowset statement handle
PDOStatement-rowCount() — Returns the number of rows affected by the last SQL statement
PDOStatement-setAttribute() — Set a statement attribute
PDOStatement-setFetchMode() — Set the default fetch mode for this statement
三、PDOException
PDO 提供了3中不同的錯誤處理策略。
1. PDO::ERRMODE_SILENT
這是默認使用的模式。PDO會在statement和database對象上設定簡單的錯誤代號,你可以使用PDO-errorCode() 和 PDO-errorInfo() 方法檢查錯誤;如果錯誤是在對statement對象進行調用時導致的,你就可以在那個對象上使用 PDOStatement-errorCode() 或 PDOStatement-errorInfo() 方法取得錯誤信息。而如果錯誤是在對database對象調用時導致的,你就應該在這個database對象上調用那兩個方法。
2. PDO::ERRMODE_WARNING
作為設置錯誤代號的附加,PDO將會發出一個傳統的E_WARNING信息。這種設置在除錯和調試時是很有用的,如果你只是想看看發生了什麼問題而不想中斷程序的流程的話。
3. PDO::ERRMODE_EXCEPTION
作為設置錯誤代號的附件,PDO會拋出一個PDOException異常並設置它的屬性來反映錯誤代號和錯誤信息。這中設置在除錯時也是很有用的,因為他會有效的“放大(blow up)”腳本中的出錯點,非常快速的指向一個你代碼中可能出錯區域。(記住:如果異常導致腳本中斷,事務處理回自動回滾。)
異常模式也是非常有用的,因為你可以使用比以前那種使用傳統的PHP風格的錯誤處理結構更清晰的結構處理錯誤,比使用安靜模式使用更少的代碼及嵌套,也能夠更加明確地檢查每個數據庫訪問的返回值。
關於PHP中異常的更多信息請看Exceptions章節
PDO 使用基於SQL-92 SQLSTATE 的錯誤代號字符串;特定的PDO驅動應當將自己本身的代號對應到適當的SQLSTATE代號上。PDO-errorCode() 方法只返回單一的SQLSTATE代號。如果你需要關於一個錯誤的更加有針對性的信息,PDO也提供了一個PDO-errorInfo()方法,它可以返回一個包含了SQLSTATE代號,特定數據庫驅動的錯誤代號和特定數據庫驅動的錯誤說明字符串。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/308483.html