① php mysqli 預處理 怎麼綁定參數
<?php
/* 連接資料庫類 MysqlConnect */
class MysqlConnect{
private $dbhost=null;
private $dbuser=null;
private $dbpwd=null;
private $dbname=null;
private $dbport=null;
private $ifpdo=null;
private $dburi=null;
private $handler=null;
function __construct($dbhost,$dbuser,$dbpwd,$dbname,$dbport,$ifpdo,$dburi){
$this->dbhost=$dbhost;
$this->dbuser=$dbuser;
$this->dbpwd=$dbpwd;
$this->dbname=$dbname;
$this->dbport=$dbport;
$this->ifpdo=$ifpdo;
$this->dburi=$dburi;//PDO的URI參數,可以查手冊
if($this->ifpdo==1){//表示調用PDO來操作資料庫
$this->handler=$this->CreatePdo();
}elseif($this->ifpdo==0){//這里可以寫MYSQLI的方法
$this->handler=null;
}
}
/* ----------------這里是入口--------------------- */
//@param sql:外部調用時傳遞的完整SQL語句
//@param bindArray:綁定的參數數組,與sql語句有關,如果沒有PDO佔位符此處為空
//@param action:傳遞操作參數,"select"/"update"/"delete"/"insert"
public function exeSql($sql,$bindArray=array(),$action=""){
$stmt=$this->handler->prepare($sql);
$stmt->execute($bindArray);
switch($action){
case "select":
return $stmt->fetch(PDO::FETCH_ASSOC);
break;
case "selectAll":
return $stmt->fetchAll(PDO::FETCH_ASSOC);
break;
case "update":
case "delete":
return $stmt->rowCount();
break;
case "insert":
return $this->handler->lastInsertId();
break;
case "count":
return $stmt->rowCount();
default:
return "";
}
}
public function query($sql){
return $this->handler->query($sql);
}
private function CreatePdo(){
try{
$handler=new PDO($this->dburi,$this->dbuser,$this->dbpwd);
$handler->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
return $handler;
}catch(PDOException $e){
$e->getMessage();
$this->handler=null;
}
}
private function __get($args){
if($args=='handler'){
return $this->handler;
}
}
}
require(NEO_A_P.'\data\sqlconfig.php');//這里是sql的連接文件,下面創建對象的時候需要的變數就是這個文件里要有的
$handler=new MysqlConnect($dbhost,$dbuser,$dbpwd,$dbname,$dbport,$ifpdo,$dburi);
?>
② php中 mysqli_fetch_array(result,resulttype)函數的第二個參
MYSQLI_ASSOC,返回結果數組為關聯數組,數組的鍵即為相應的欄位名,你使用時可以像這樣:
$row['id']、$row['name'].....
MYSQLI_NUM,返回的結果數組為索引數組,數組的鍵即為0,1,2,3,4這樣的數字索引,你使用時只能這樣:
$row[0]、row[1].....
MYSQLI_BOTH,返回的結果數組中同時存在欄位索引和數字索引,你使用時,上面兩種都可以用。
一般常用前兩個。
③ php語言中的mysqli_query
php語言中的mysqli_query() 函數執行某個針對資料庫的查詢。
語法
mysqli_query(connection,query,resultmode);
參數 描述
connection 必需。規定要使用的 MySQL 連接。
query 必需,規定查詢字元串。
resultmode 可選。一個常量。可以是下列值中的任意一個:
MYSQLI_USE_RESULT(如果需要檢索大量數據,請使用這個)
MYSQLI_STORE_RESULT(默認)
技術細節
返回值:針對成功的 SELECT、SHOW、DESCRIBE 或 EXPLAIN 查詢,將返回一個 mysqli_result 對象。針對其他成功的查詢,將返回 TRUE。如果失敗,則返回 FALSE。
PHP 版本:5+
更新日誌:在 PHP 5.3.0 中新增了非同步查詢的功能。
實例
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
//Checkconnection
if(mysqli_connect_errno($con))
{
echo"FailedtoconnecttoMySQL:".mysqli_connect_error();
}
//Performqueries
mysqli_query($con,"SELECT*
FROMPersons");
mysqli_query($con,"INSERTINTOPersons(FirstName,LastName,Age)
VALUES('Glenn','Quagmire',33)");
mysqli_close($con);
?>
④ 在PHP的mysqli擴展中,連接資料庫的函數是什麼
mysqli_connect,手冊上的例子如下圖:
⑤ php新手問題 mysqli_query參數為空
$link 是連接資料庫的吧,你寫在哪裡了?沒有連接怎麼執行呀
⑥ php使用mysqli向資料庫添加數據的方法
本文實例講述了php使用mysqli向資料庫添加數據的方法。分享給大家供大家參考。具體實現方法如下:
$mydb
=
new
mysqli('localhost',
'username',
'password',
'databasename');
$sql
=
"INSERT
INTO
users
(fname,
lname,
comments)
VALUES
('$_POST[fname]',
'$_POST[lname]',
'$_POST[comments]')";
if
($mydb->query($sql)
==
TRUE)
{
echo
"user
entry
saved
successfully.";
}
else
{
echo
"INSERT
attempt
failed"
;
}
$mydb->close();
希望本文所述對大家的php程序設計有所幫助。
⑦ php中mysqli處理查詢結果集的幾個方法
$sql="select * from user"; $result=$link->query($sql); $row=$result->fetch_all(MYSQLI_BOTH);//參數MYSQL_ASSOC、MYSQLI_NUM、MYSQLI_BOTH規定產生數組類型
$n=0; while($n<mysqli_num_rows($result)){ echo "ID:".$row[$n]["id"]."用戶名:".$row[$n]["name"]."密碼:".$row[$n]["password"]."<br />"; $n++;
}
⑧ php怎麼用mysqli鏈接資料庫和輸出sql
一、mysql與mysqli的概念相關:
1、mysql與mysqli都是php方面的函數集,與
mysql資料庫
關聯不大。
2、在
php5
版本之前,一般是用php的
mysql函數
去驅動mysql資料庫的,比如mysql_query()的函數,屬於
面向過程
3、在php5版本以後,增加了mysqli的函數功能,某種意義上講,它是mysql系統函數的增強版,更穩定更高效更安全,與mysql_query()對應的有mysqli_query(),屬於面向對象,用對象的方式操作驅動mysql資料庫
二、mysql與mysqli的區別:
1、mysql是非持繼連接函數,mysql每次鏈接都會打開一個連接的進程。
2、mysqli是永遠連接函數,mysqli多次運行mysqli將使用同一連接進程,從而減少了伺服器的開銷。mysqli封裝了諸如事務等一些高級操作,同時封裝了DB操作過程中的很多可用的方法。
三、mysql與mysqli的用法:
1:mysql(過程方式):
$conn
=
mysql_connect('
localhost
',
'user',
'password');//連接mysql資料庫
mysql_select_db
('data_base');
//選擇資料庫$result
=
mysql_query('select
*
from
data_base');//第二個可選參數,指定打開的連接$row
=
mysql_fetch_row(
$result
)
)
//只取一行數據echo
$row[0];
//輸出第一個欄位的值
PS:mysqli以過程式的方式操作,有些函數必須指定資源,比如mysqli_query(資源標識,
SQL語句
),並且資源標識的參數是放在前面的,而mysql_query(SQL語句,'資源標識')的資源標識是可選的,默認值是上一個打開的連接或資源。
2、mysqli(對象方式):
$conn
=
new
mysqli('localhost',
'user',
'password','data_base');//要使用new
操作符
,最後一個參數是直接指定資料庫//假如構造時候不指定,那下一句需要$conn
->
select_db('data_base')實現$result
=
$conn
->
query(
'select
*
from
data_base'
);$row
=
$result
->
fetch_row();
//取一行數據echo
row[0];
//輸出第一個欄位的值
使用new
mysqli('localhost',
usenamer',
'password',
'databasename');會報錯,提示如下:
Fatal
error:
Class
'mysqli'
not
found
in
...
一般是mysqli是沒有開啟的,因為mysqli類不是
默認開啟
的,win下要改php.ini,去掉php_mysqli.dll前的;,linux下要把mysqli編譯進去。
四、mysql_connect()與mysqli_connect()
1.使用mysqli,可以把資料庫名稱當作參數傳給mysqli_connect()函數,也可以傳遞給mysqli的
構造函數
;
2.如果調用mysqli_query()或mysqli的對象查詢query()方法,則連接標識是必需的。
⑨ php mysqli 常用函數有哪些
php 中mysqli是個類,這個類的函數(方法)有:
mysqli::$affected_rows — Gets the number of affected rows in a previous MySQL operation
mysqli::autocommit — 打開或關閉本次資料庫連接的自動命令提交事務模式
mysqli::begin_transaction — Starts a transaction
mysqli::change_user — Changes the user of the specified database connection
mysqli::character_set_name — 返回當前資料庫連接的默認字元編碼
mysqli::$client_info — Get MySQL client info
mysqli::$client_version — Returns the MySQL client version as a string
mysqli::close — 關閉先前打開的資料庫連接
mysqli::commit — 提交一個事務
mysqli::$connect_errno — Returns the error code from last connect call
mysqli::$connect_error — Returns a string description of the last connect error
mysqli::__construct — Open a new connection to the MySQL server
mysqli::debug — Performs debugging operations
mysqli::mp_debug_info — 將調試信息輸出到日誌
mysqli::errno — 返回最近函數調用的錯誤代碼
mysqli::$error_list — Returns a list of errors from the last command executed
mysqli::$error — Returns a string description of the last error
mysqli::$field_count — Returns the number of columns for the most recent query
mysqli::get_charset — Returns a character set object
mysqli::get_client_info — Get MySQL client info
mysqli_get_client_stats — Returns client per-process statistics
mysqli_get_client_version — 作為一個整數返回MySQL客戶端的版本
mysqli::get_connection_stats — Returns statistics about the client connection
mysqli::$host_info — 返回一個表述使用的連接類型的字元串
mysqli::$protocol_version — 返回MySQL使用的協議版本號
mysqli::$server_info — 返回MySQL伺服器的版本號
mysqli::$server_version — 作為一個整數返回MySQL伺服器的版本
mysqli::get_warnings — Get result of SHOW WARNINGS
mysqli::$info — Retrieves information about the most recently executed query
mysqli::init — Initializes MySQLi and returns a resource for use with mysqli_real_connect()
mysqli::$insert_id — Returns the auto generated id used in the last query
mysqli::kill — Asks the server to kill a MySQL thread
mysqli::more_results — Check if there are any more query results from a multi query
mysqli::multi_query — Performs a query on the database
mysqli::next_result — Prepare next result from multi_query
mysqli::options — Set options
mysqli::ping — Pings a server connection, or tries to reconnect if the connection has gone down
mysqli::poll — Poll connections
mysqli::prepare — Prepare an SQL statement for execution
mysqli::query — 對資料庫執行一次查詢
mysqli::real_connect — 建立一個 MySQL 伺服器連接
mysqli::real_escape_string — Escapes special characters in a string for use in an SQL statement, taking into account the current charset of the connection
mysqli::real_query — 執行一個mysql查詢
mysqli::reap_async_query — Get result from async query
mysqli::refresh — Refreshes
mysqli::release_savepoint — Removes the named savepoint from the set of savepoints of the current transaction
mysqli::rollback — 回退當前事務
mysqli::rpl_query_type — Returns RPL query type
mysqli::savepoint — Set a named transaction savepoint
mysqli::select_db — 選擇用於資料庫查詢的默認資料庫
mysqli::send_query — 發送請求並返回結果
mysqli::set_charset — 設置默認字元編碼
mysqli::set_local_infile_default — Unsets user defined handler for load local infile command
mysqli::set_local_infile_handler — Set callback function for LOAD DATA LOCAL INFILE command
mysqli::$sqlstate — Returns the SQLSTATE error from previous MySQL operation
mysqli::ssl_set — Used for establishing secure connections using SSL
mysqli::stat — Gets the current system status
mysqli::stmt_init — 初始化一條語句並返回一個用於mysqli_stmt_prepare(調用)的對象
mysqli::store_result — Transfers a result set from the last query
mysqli::$thread_id — Returns the thread ID for the current connection
mysqli::thread_safe — 返回是否是線程安全的
mysqli::use_result — Initiate a result set retrieval
mysqli::$warning_count — Returns the number of warnings from the last query for the given link
以上函數清單直接來自www.php.net網站。你可以進入該網站參看。