首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP和SQL Server -思考

问PHP和SQL Server -思考
EN

Stack Overflow用户
提问于 2011-10-07 08:01:24
回答 1查看 948关注 0票数 4

我最近接受了一个性质独特的项目,希望从每个人那里获得一些建议。

我分别使用asp.net/SQL Server和php/mysql。我从来没有把它们混在一起。然而,我当前的项目要求我在使用SQL Server后端时使用PHP编写代码。

我找到了很多关于如何连接到SQL Server的文章,但我想在这里问一个问题。有什么问题吗?

在SQL Server后端使用PHP和使用MySQL有什么不同?

EN

回答 1

Stack Overflow用户

发布于 2011-10-07 08:17:25

我曾经将一个项目从PHP/MySql转换为PHP/MSSQL。我将在这里提供我在转换过程中发现的笔记。我认为出于某种原因,我最终使用odbc命令而不是mssql命令,因为它们似乎更可靠,特别是当您在同一个页面中执行多个查询时-不要问我为什么,这就是我所发现的。

我的命令语法对比图:

代码语言:javascript
复制
*mysql cmd*         *mssql cmd*         *odbc cmd*          *Notes*
mysql_errno         mssql_errno         odbc_error  
mysql_error         mssql_error         odbc_errormsg   
mysql_select_db     mssql_select_db     unneeded    
mysql_query         mssql_query         odbc_exec           odbc_exec requires the resource link parameter ($db)
mysql_affected_rows mssql_rows_affected odbc_num_rows   
mysql_num_rows      mssql_num_rows      odbc_num_rows   
mysql_fetch_object  mssql_fetch_object  odbc_fetch_object   
mysql_close         mssql_close         odbc_close          odbc_close requires the resource link parameter ($db)
mysql_fetch_array   mssql_fetch_array   odbc_fetch_array    
mysql_result        mssql_result        odbc_result         odbc_result cannot take a row index as parameter; must cycle with odbc_fetch_row
mysql_fetch_row     mssql_fetch_row     odbc_fetch_row      odbc_fetch_row does not return the result; use odbc_fetch_row with odbc_result

我的代码片段替换图用于转换(我实际上是通过生成一个查找/替换字符串列表并将它们应用于整个代码库来完成这个项目的:-):

代码语言:javascript
复制
*old mysql code*                                        *new odbc code*                             *Notes*
for ($i = 0; $i < mysql_num_rows($result); $i++)        while (odbc_fetch_row($result))             odbc_num_rows doesn't usually work for finding how many rows returned
mysql_result($result, $i                                odbc_result($result                         odbc_result can't go request the result for a specific row, have to use odbc_fetch_row
NOW()                                               GETDATE()                                   NOW() function in mysql is GETDATE() in sql server
if (connect_db())                                   if ($db = connect_db())                     In mysql, you don't have to keep track of the $db resource - with odbc, you do
if (!connect_db())                                  if (!($db = connect_db()))                  See notes on previous entry
                                                    odbc_fetch_row($result);                    When retrieving a single row, you have to call fetch_row with odbc, but not with mysql
if (mysql_num_rows($result) == 1)                   if (odbc_fetch_row($result))                odbc_num_rows usually doesn't work, so for a single row, just do if odbc_fetch_row
if (mysql_errormsg() || mysql_num_rows($ASISHSresult) == 0)     if (!odbc_errormsg() && !odbc_fetch_row($ASISHSresult)) 
limit ##, ##                                        row_number() over (order by ???)            limit function in mysql has to be translated to row_number() function in odbc (also must be inc. by 1 to offset off-by-1 error between mysql and sql server)
match(body) against ('expr' in boolean mode)        contains(body, 'expr') or containstable(Body, 'expr')   fulltext matching in sql server has different syntax than mysql
text_column = value                                 cast(text_column as varchar(good_size)) = value     mysql can compare text column to value, sql server cannot without varchar casting
                                                    odbc_free_result($result);                  When you make a lot of connections, you must free results or your future connections may be ignored

最后一点。如果你使用内置的加密命令来加密密码,就像我们在这个项目中所做的那样,你需要在php而不是mysql中进行加密。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7681831

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档