導航:首頁 > 編程語言 > phpmysqlndmysql

phpmysqlndmysql

發布時間:2022-05-02 16:46:05

1. php怎麼連接mysql資料庫

php連接mysql 分為面向過程和面向對象的。

以下三種方式都可以連接

一 MySQLi - 面向對象

<?php
$servername="localhost";
$username="username";
$password="password";
//創建連接$conn=newmysqli($servername,$username,$password);
//檢測連接if($conn->connect_error){
die("連接失敗:".$conn->connect_error);}echo"連接成功";
?>

二 MySQLi - 面向過程

<?php
$servername="localhost";
$username="username";
$password="password";
//創建連接$conn=mysqli_connect($servername,$username,$password);
//檢測連接if(!$conn){
die("Connectionfailed:".mysqli_connect_error());}echo"連接成功";
?>

三 PDO方式

<?php
$servername="localhost";
$username="username";
$password="password";
try{
$conn=newPDO("mysql:host=$servername;dbname=myDB",$username,$password);
echo"連接成功";
}catch(PDOException$e){
echo$e->getMessage();
}
?>

連接在腳本執行完後會自動關閉。你也可以使用以下代碼來關閉連接:

MySQLi - 面向對象

$conn->close();



MySQLi - 面向過程

mysqli_close($conn);



PDO

$conn = null;

2. PHP + Mysql多個表並行查詢如何實現

在PHP-FPM處理HTTP請求時,有時會遇到一個請求需要進行多次MySQL查詢(在報表類應用中比較常見)。通常我們會以串列方式查詢:

$link=newmysqli();
$rs1=$link->query('SELECT*FROMtable1');
while($row=$rs1->fetch_row()){...}
$rs2=$link->query('SELECT*FROMtable2');
while($row=$rs2->fetch_row()){...}
$rs3=$link->query('SELECT*FROMtable3');
while($row=$rs3->fetch_row()){...}

串列查詢方式有個缺點:在MySQL返回數據之前,PHP一直是處於空等的狀態,不會繼續往後執行。如果數據量大或者查詢復雜,MySQL響應可能會比較慢,那麼以串列方式查詢會有一些延遲。給用戶最直接的感受就是 Loading… 的圈圈一直打轉。

那麼有什麼辦法可以減少查詢MySQL的時間?用多進程並行查詢不行,因為PHP-FPM 中不允許用 pcntl_fork 一類的調用。

幸好還有 mysqlnd,mysqlnd提供了類似 stream_select 的機制(見 這篇文章) ,可以做到在單進程中對MySQL並行查詢。這主要運用了mysqli_poll 和 reap_async_query 兩個函數。

還是通過例子來介紹MySQL並行查詢的實施方法。假設要並行地向MySQL發出10個查詢,最基本的代碼應該是這樣的:

1.$links=[];
2.for($i=0;$i!==10;$i++){
3.$links[$i]=newmysqli('127.0.0.1','user','password','db1');
4.$links[$i]->query('SELECTSLEEP(1)',MYSQLI_ASYNC);
5.}
6.$allResult=[];
7.while(!empty($links)){
8.$reads=$links;
9.$errors=$reject=[];
10.if(!mysqli_poll($reads,$errors,$reject,null)){
11.continue;
12.}
13.foreach($readsas$read){
14.$idx=array_search($read,$links,true);
15.$allResult[$idx]=[];
16.$result=$read->reap_async_query();
17.while($row=$result->fetch_row()){
18.$allResult[$idx][]=$row;
19.}
20.$read->close();
21.unset($links[$idx]);
22.}
23.}

解釋下這段代碼的含義:

2~5行,同時發起10個MySQL連接,並發出查詢

注意query() 的第二個參數帶上了 MYSQLI_ASYNC 表示非阻塞查詢

10行,使用mysqli_poll 輪詢10個連接的查詢有無返回

mysqli_poll 的第一個參數$reads是個數組,包含需要輪詢那些連接。mysqli_poll 執行完後,會改寫$reads,改寫後$reads包含的是那些已經有數據返回連接。

mysqli_poll的第四個參數,控制的是輪詢的等待時間,單位是「秒」。如果像本例當中設置為null,那麼mysqli_poll輪詢是阻塞的:只有監聽的連接中,任意一個連接有數據返回了,mysqli_poll才會返回。如果等待時間設置為0,那麼每次執行mysqli_poll會立即返回,外層的while會頻繁循環。

第11~19行,遍歷已經有數據返回的連接

reap_async_query和普通query一樣,返回的是mysqli_result,可以一行行fetch數據

20~21行,對於已經獲得了數據的連接,下次mysqli_poll就不需要再輪詢這個連接了,所以關閉連接,並從$links數組刪除這個連接

當所有的連接都返回了數據,$links數組空了,while循環也就終止了。

使用並行查詢的方式,可以大大縮短處理HTTP請求的時間,假設本例中的10個SQL查詢,每個需要執行1秒。因為是並行,處理所有的查詢,也只需要1秒左右。

3. PHP中MySQLi和MySQL是一回事嗎

mysql與mysqli都是php方面的函數集,與mysql資料庫關聯不大。
在php5版本以後,增加了mysqli的函數功能,某種意義上講,它是mysql系統函數的增強版

4. 關於php中mysql mysqli 區別

PHP中mysql有兩個概念,一個是mysql資料庫,一個是用於操作mysql資料庫的擴展(PHP的各種功能都是通過這些底層擴展來實現的)。而你這個問題中的mysql,就指的是擴展。
PHP5開始有了mysqli,按照PHP官方描述,它是mysql增強版擴展。事實上它確實更搞笑更安全,並推薦大家使用。到PHP5.3的時候,原來的mysql擴展已經被標注為過時。而到了PHP7,原mysql擴展被徹底廢棄。
所以不管是否使用PHP7,都建議使用mysqli或pdo擴展來操作mysql資料庫。
另外,mysqli並不是簡單的在原來mysql的方法上加一個i,它是自己一套方法。mysqli同時支持面向過程和面向對象的方式,強烈建議你學者使用面向對象的方式。

5. php mysql與mysqli 區別

1、mysql是非持繼連接函數,mysql每次鏈接都會打開一個連接的進程。
2、mysqli是永遠連接函數,mysqli多次運行mysqli將使用同一連接進程,從而減少了伺服器的開銷。mysqli封裝了諸如事務等一些高級操作,同時封裝了DB操作過程中的很多可用的方法。
mysqli連接是永久連接,而mysql是非永久連接。
mysql連接:每當第二次使用的時候,都會重新打開一個新的進程。
mysqli連接:一直都只使用同一個進程。

6. PHP mysql資料庫問題

1. mysql操作語句環境有兩種,1種是函數形式的,如mysql_query,另一種是面向對象形式的如$mysqli = new Mysqli($host,$username,$pwd);,第二種所有操作都是以php 面向對象的形式操作mysql資料庫的。如mysqli->query('sql');等同於mysql_qeury('sql')
查看伺服器是否開啟mysqli擴展的方式可以通過<?php echo phpinfo() ?>看是否存在mysqli擴展
2.獲取插入數據的id方法是mysql_insert_id($query);獲取mysqli->insert_id;要獲取插入數據id,id必須設置為自動增加模式(auto_increment).
$msyqli = new Mysqli($host,$user,$pwd);
$mysqli->select_db($dbname);
$mysqli->query('set names gbk');
$mysqli->query('insert into tb123(body)values("test")');
echo $mysql->insert_id;
$mysqli->close();
最後,請給點分數,獎勵一下啊

7. PHP7.0怎麼通過打開擴展功能和mysql相連

第一步:進入php源碼中的"ext/mysql"目錄下

第二步:在當前目錄下運行phpize命令:/usr/local/php524/bin/phpize

phpize的規則:去哪個目錄下運行phpize文件,那麼就會在該目錄下生成一個configure文件。

第三步:運行剛才生成的configure文件

命令: ./configure --with-php-config=/usr/local/php524/bin/php-config --with-mysql=/usr/local/mysql/

這里最關鍵的是通過--with-mysql參數告訴mysql客戶端的位置。這樣才能生成mysql.so。

實驗的時候,沒有加這個參數,結果錯誤:

./configure --with-php-config=/usr/local/php524/bin/php-config

第四步:編譯生成.so文件

第五步:配置php引擎載入該擴展。

補充一下:就是去php.ini文件中修改一下配置,載入mysql.so這個擴展(這個擴展文件要放到php指定的擴展目錄下面去)

第六步:測試php引擎是否成功載入該擴展編寫文件phpinfo.php,內容是:<?phpehco phpinfo();?>

運行後,可以看到有如下信息顯示:mysqlMySQLSupport enabledActive PersistentLinks 0

Active Links 0

Client API version 5.1.55

MYSQL_MODULE_TYPE no value

MYSQL_SOCKET /tmp/mysql.sock

MYSQL_INCLUDE no value

MYSQL_LIBS no value

通過這樣的方式可以確認,php引擎已經成功載入了mysql.so擴展。

第七步:已經生成的mysql.so。編寫php代碼測試是否能連接mysql。

8. 如何使用php登錄mysql,使用mysqli的登錄方式,並插入一條數據,誰有有完整的php原代碼

本文所述的是一個在PHP中以mysqli方式連接資料庫的一個資料庫類實例,該資料庫類是從一個PHP的CMS中整理出來的,可實現PHP連接資料庫類,MySQLi版,兼容PHP4,對於有針對性需要的朋友可根據此代碼進行優化和修改。
?

<?php
#==================================================================================================
# Filename: /db/db_mysqli.php
# Note : 連接資料庫類,MySQLi版
#==================================================================================================
#[類庫sql]
class db_mysqli
{
var $query_count = 0;
var $host;
var $user;
var $pass;
var $data;
var $conn;
var $result;
var $prefix = "qinggan_";
//返回結果集類型,默認是數字+字元
var $rs_type = MYSQLI_ASSOC;
var $query_times = 0;#[查詢時間]
var $conn_times = 0;#[連接資料庫時間]
var $unbuffered = false;
//定義查詢列表
var $querylist;
var $debug = false;
#[構造函數]
function __construct($config=array())
{
$this->host = $config['host'] ? $config['host'] : 'localhost';
$this->port = $config['port'] ? $config['port'] : '3306';
$this->user = $config['user'] ? $config['user'] : 'root';
$this->pass = $config['pass'] ? $config['pass'] : '';
$this->data = $config['data'] ? $config['data'] : '';
$this->debug = $config["debug"] ? $config["debug"] : false;
$this->prefix = $config['prefix'] ? $config['prefix'] : 'qinggan_';
if($this->data)
{
$ifconnect = $this->connect($this->data);
if(!$ifconnect)
{
$this->conn = false;
return false;
}
}
return true;
}
#[兼容PHP4]
function db_mysqli($config=array())
{
return $this->__construct($config);
}
#[連接資料庫]
function connect($database="")
{
$start_time = $this->time_used();
if(!$this->port) $this->port = "3306";
$this->conn = @mysqli_connect($this->host,$this->user,$this->pass,"",$this->port) or false;
if(!$this->conn)
{
return false;
}
$version = $this->get_version();
if($version>"4.1")
{
mysqli_query($this->conn,"SET NAMES 'utf8'");
if($version>"5.0.1")
{
mysqli_query($this->conn,"SET sql_mode=''");
}
}
$end_time = $this->time_used();
$this->conn_times += round($end_time - $start_time,5);#[連接資料庫的時間]
$ifok = $this->select_db($database);
return $ifok ? true : false;
}
function select_db($data="")
{
$database = $data ? $data : $this->data;
if(!$database)
{
return false;
}
$this->data = $database;
$start_time = $this->time_used();
$ifok = mysqli_select_db($this->conn,$database);
if(!$ifok)
{
return false;
}
$end_time = $this->time_used();
$this->conn_times += round($end_time - $start_time,5);#[連接資料庫的時間]
return true;
}
#[關閉資料庫連接,當您使用持續連接時該功能失效]
function close()
{
if(is_resource($this->conn))
{
return mysqli_close($this->conn);
}
else
{
return true;
}
}
function __destruct()
{
return $this->close();
}
function set($name,$value)
{
if($name == "rs_type")
{
$value = strtolower($value) == "num" ? MYSQLI_NUM : MYSQLI_ASSOC;
}
$this->$name = $value;
}
function query($sql)
{
if(!is_resource($this->conn))
{
$this->connect();
}
else
{
if(!mysql_ping($this->conn))
{
$this->close();
$this->connect();
}
}
if($this->debug)
{
$sqlkey = md5($sql);
if($this->querylist)
{
$qlist = array_keys($this->querylist);
if(in_array($sqlkey,$qlist))
{
$count = $this->querylist[$sqlkey]["count"] + 1;
$this->querylist[$sqlkey] = array("sql"=>$sql,"count"=>$count);
}else{
$this->querylist[$sqlkey] = array("sql"=>$sql,"count"=>1);
}
}
else{
$this->querylist[$sqlkey] = array("sql"=>$sql,"count"=>1);
}
}
$start_time = $this->time_used();
$func = $this->unbuffered && function_exists("mysqli_multi_query") ? "mysqli_multi_query" : "mysqli_query";
$this->result = @$func($this->conn,$sql);
$this->query_count++;
$end_time = $this->time_used();
$this->query_times += round($end_time - $start_time,5);#[查詢時間]
if(!$this->result)
{
return false;
}
return $this->result;
}
function get_all($sql="",$primary="")
{
$result = $sql ? $this->query($sql) : $this->result;
if(!$result)
{
return false;
}
$start_time = $this->time_used();
$rs = array();
$is_rs = false;
while($rows = mysqli_fetch_array($result,$this->rs_type))
{
if($primary && $rows[$primary])
{
$rs[$rows[$primary]] = $rows;
}
else
{
$rs[] = $rows;
}
$is_rs = true;
}
$end_time = $this->time_used();
$this->query_times += round($end_time - $start_time,5);#[查詢時間]
return ($is_rs ? $rs : false);
}
function get_one($sql="")
{
$start_time = $this->time_used();
$result = $sql ? $this->query($sql) : $this->result;
if(!$result)
{
return false;
}
$rows = mysqli_fetch_array($result,$this->rs_type);
$end_time = $this->time_used();
$this->query_times += round($end_time - $start_time,5);#[查詢時間]
return $rows;
}
function insert_id($sql="")
{
if($sql)
{
$rs = $this->get_one($sql);
return $rs;
}
else
{
return mysqli_insert_id($this->conn);
}
}
function insert($sql)
{
$this->result = $this->query($sql);
$id = $this->insert_id();
return $id;
}
function all_array($table,$condition="",$orderby="")
{
if(!$table)
{
return false;
}
$table = $this->prefix.$table;
$sql = "SELECT * FROM ".$table;
if($condition && is_array($condition) && count($condition)>0)
{
$sql_fields = array();
foreach($condition AS $key=>$value)
{
$sql_fields[] = "`".$key."`='".$value."' ";
}
$sql .= " WHERE ".implode(" AND ",$sql_fields);
}
if($orderby)
{
$sql .= " ORDER BY ".$orderby;
}
$rslist = $this->get_all($sql);
return $rslist;
}
function one_array($table,$condition="")
{
if(!$table)
{
return false;
}
$table = $this->prefix.$table;
$sql = "SELECT * FROM ".$table;
if($condition && is_array($condition) && count($condition)>0)
{
$sql_fields = array();
foreach($condition AS $key=>$value)
{
$sql_fields[] = "`".$key."`='".$value."' ";
}
$sql .= " WHERE ".implode(" AND ",$sql_fields);
}
$rslist = $this->get_one($sql);
return $rslist;
}
//將數組寫入數據中
function insert_array($data,$table,$insert_type="insert")
{
if(!$table || !is_array($data) || !$data)
{
return false;
}
$table = $this->prefix.$table;//自動增加表前綴
if($insert_type == "insert")
{
$sql = "INSERT INTO ".$table;
}
else
{
$sql = "REPLACE INTO ".$table;
}
$sql_fields = array();
$sql_val = array();
foreach($data AS $key=>$value)
{
$sql_fields[] = "`".$key."`";
$sql_val[] = "'".$value."'";
}
$sql.= "(".(implode(",",$sql_fields)).") VALUES(".(implode(",",$sql_val)).")";
return $this->insert($sql);
}
//更新數據
function update_array($data,$table,$condition)
{
if(!$data || !$table || !$condition || !is_array($data) || !is_array($condition))
{
return false;
}
$table = $this->prefix.$table;//自動增加表前綴
$sql = "UPDATE ".$table." SET ";
$sql_fields = array();
foreach($data AS $key=>$value)
{
$sql_fields[] = "`".$key."`='".$value."'";
}
$sql.= implode(",",$sql_fields);
$sql_fields = array();
foreach($condition AS $key=>$value)
{
$sql_fields[] = "`".$key."`='".$value."' ";
}
$sql .= " WHERE ".implode(" AND ",$sql_fields);
return $this->query($sql);
}
function count($sql="")
{
if($sql)
{
$this->rs_type = MYSQLI_NUM;
$this->query($sql);
$rs = $this->get_one();
$this->rs_type = MYSQLI_ASSOC;
return $rs[0];
}
else
{
return mysqli_num_rows($this->result);
}
}
function num_fields($sql="")
{
if($sql)
{
$this->query($sql);
}
return mysqli_num_fields($this->result);
}
function list_fields($table)
{
$rs = $this->get_all("SHOW COLUMNS FROM ".$table);
if(!$rs)
{
return false;
}
foreach($rs AS $key=>$value)
{
$rslist[] = $value["Field"];
}
return $rslist;
}
#[顯示表名]
function list_tables()
{
$rs = $this->get_all("SHOW TABLES");
return $rs;
}
function table_name($table_list,$i)
{
return $table_list[$i];
}
function escape_string($char)
{
if(!$char)
{
return false;
}
return mysqli_escape_string($this->conn,$char);
}
function get_version()
{
return mysqli_get_server_info($this->conn);
}
function time_used()
{
$time = explode(" ",microtime());
$used_time = $time[0] + $time[1];
return $used_time;
}
//Mysql的查詢時間
function conn_times()
{
return $this->conn_times + $this->query_times;
}
//MySQL查詢資料
function conn_count()
{
return $this->query_count;
}
# 高效SQL生成查詢,僅適合單表查詢
function phpok_one($tbl,$condition="",$fields="*")
{
$sql = "SELECT ".$fields." FROM ".$this->db->prefix.$tbl;
if($condition)
{
$sql .= " WHERE ".$condition;
}
return $this->get_one($sql);
}
function debug()
{
if(!$this->querylist || !is_array($this->querylist) || count($this->querylist) < 1)
{
return false;
}
$html = '<table cellpadding="0" cellspacing="0" width="100%" bgcolor="#CECECE"><tr><td>';
$html.= '<table cellpadding="1" cellspacing="1" width="100%">';
$html.= '<tr><th bgcolor="#EFEFEF" height="30px">SQL</th><th bgcolor="#EFEFEF" width="80px">查詢</th></tr>';
foreach($this->querylist AS $key=>$value)
{
$html .= '<tr><td bgcolor="#FFFFFF"><div style="padding:3px;color:#6E6E6E;">'.$value['sql'].'</div></td>';
$html .= '<td align="center" bgcolor="#FFFFFF"><div style="padding:3px;color:#000000;">'.$value["count"].'</div></td></tr>';
}
$html.= "</table>";
$html.= "</td></tr></table>";
return $html;
}
function conn_status()
{
if(!$this->conn) return false;
return true;
}
}
?>

9. PHP5.5 win7系統,phpinfo()里只有mysqlnd沒有mysql 求問

mysqlnd 只是mysql的驅動。需要把mydql擴展配置好才可以。查看php.ini
extension=php_mysql.dll
前面的「;」分號是否去掉,記得看一下擴展文件php_mysql.dll是不是在php的擴展目錄里。
再重啟一下apache或者nginx

閱讀全文

與phpmysqlndmysql相關的資料

熱點內容
grub2命令行 瀏覽:618
無法獲取加密卡信息 瀏覽:774
雲伺服器網卡充值 瀏覽:509
編程就是軟體 瀏覽:49
伺服器如何添加許可權 瀏覽:437
引用指針編程 瀏覽:851
手機加密日記本蘋果版下載 瀏覽:63
命令行括弧 瀏覽:176
java程序升級 瀏覽:490
排序演算法之插入類 瀏覽:227
gcccreate命令 瀏覽:73
海爾監控用什麼app 瀏覽:64
系統盤被壓縮開不了機 瀏覽:984
linuxredis30 瀏覽:541
狸窩pdf轉換器 瀏覽:696
ajax調用java後台 瀏覽:904
活塞式壓縮機常見故障 瀏覽:614
break演算法 瀏覽:731
換電池的app是什麼 瀏覽:771
單片機ad采樣快速發送電腦 瀏覽:22