我現在開始使用帶有mssql的PHP 7,並且需要將sql函數從mssql_切換到sqlsrv_。當我使用mssql函數時,我正在使用函數mssql_change_db更改數據庫,但正如我現在看到的,沒有替代函數可用於替代此函數。我想通過在每個查詢中使用USE [DB]來讓這個函數自己買,但它似乎是無用或無效的。我想過另一種方式,這似乎更好。
我想過的另一種方法是在每個表名稱之前添加DB_NAME.dbo.table。
我也開始建立一個這樣的函數,它查找表名並在表名前加上「.dbo」自動添加數據庫名。
這是我到現在爲止所做的:(我剛開始建造它)
這只是一個例子,詛咒它還沒有完成。
使用sqlsrv切換數據庫的正確方法是什麼?
public function exec($mainQuery, $db = NULL)
{
if(isset($db)) {
$query = $mainQuery;
$query = explode('from ', $query);
$query = $query[1]; // Getting the rest of the query after the word "Form"
if (strpos($query, 'where') !== false) { // Checking if the rest of the query includes the word "Where"
$query = explode('where', $query); // Taking the word before the word "Where"
$tableName = $query[0];
}
else { // The rest of the query is actually the table name, because there is no "Where" in the query
$tableName = $query;
}
$pQuery = str_replace("$tableName", " $db.dbo.$tableName", $mainQuery); // Replacing the table name with the DB.dbo.tablename
return sqlsrv_query($this -> sqlHandle, $pQuery);
}
else {
return sqlsrv_query($this -> sqlHandle, $mainQuery);
}
}
我應該建立也爲JOIN,INSERT(INTO),UPDATE。
我不知道爲什麼,但我覺得這是不好的方法,
這就是爲什麼我問你什麼是切換數據庫的更好方法。
在此先感謝。
感謝您回答我的問題,關鍵是沒有「默認數據庫」在我的情況下,我使用6個數據庫,並且我在編碼的每個代碼頁中都使用它們,我沒有我自己的數據庫,這是插入到它的數據。謝謝,我想我會繼續前進。 – guyshitz