【问题标题】:Change code from mysql to mssql in a php file在 php 文件中将代码从 mysql 更改为 mssql
【发布时间】:2019-06-24 12:34:05
【问题描述】:

我是 php 和 sql 的新手。

以下代码是用 php 编写的,在连接到 mysql 数据库时可以正常工作。 SELECT 查询正在运行,而 UPDATE 查询正在运行。

但是当连接到 mssql 数据库时,代码不能正常工作。 我需要将它们转换为连接到类似的 mssql 数据库。 谢谢。


 <table id="data_table" class="table table-striped">
    <thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Gender</th>
            <th>Age</th>    
            <th>Designation</th>
            <th>Address</th>
        </tr>
    </thead>
    <tbody>
        <?php 
        $sql_query = "SELECT id, firstname, lastname, address, email FROM 
     myguests LIMIT 10";
        $resultset = mysqli_query($conn, $sql_query) or die("database 
     error:". mysqli_error($conn));
        while( $developer = mysqli_fetch_assoc($resultset) ) {
        ?>
           <tr id="<?php echo $developer ['id']; ?>">
           <td><?php echo $developer ['id']; ?></td>
           <td><?php echo $developer ['firstname']; ?></td>
           <td><?php echo $developer ['lastname']; ?></td>
           <td><?php echo $developer ['address']; ?></td>   
           <td><?php echo $developer ['email']; ?></td>

               </tr>
            <?php } ?>
        </tbody>
      </table>  

<?php
/* Database connection start */
$servername = "*****";
$username = "*****";
$password = "*****";
$dbname = "*****";
  $conn = mysqli_connect($servername, $username, $password, $dbname) or 
  die("Connection failed: " . mysqli_connect_error());
 if (mysqli_connect_errno()) {
   printf("Connect failed: %s\n", mysqli_connect_error());
  exit();
  }
 ?>

   <?php
   include_once("db_connect.php");
   $input = filter_input_array(INPUT_POST);
   if ($input['action'] == 'edit') {    
    $update_field='';
   if(isset($input['firstname'])) {
    $update_field.= "firstname='".$input['firstname']."'";
    } else if(isset($input['lastname'])) {
    $update_field.= "lastname='".$input['lastname']."'";
   } else if(isset($input['address'])) {
    $update_field.= "address='".$input['address']."'";
     } else if(isset($input['email'])) {
    $update_field.= "email='".$input['email']."'";

       }    
        if($update_field && $input['id']) {
        $sql_query = "UPDATE myguests SET $update_field WHERE id='" . 
          $input['id'] . "'";   
            mysqli_query($conn, $sql_query) or die("database error:". 
          mysqli_error($conn));     
              }
              }

【问题讨论】:

  • 详细了解您遇到的具体转换问题或错误。有些尝试不会出错。其他人可能会觉得你只是在传递你的重写工作。
  • 您打算如何连接到 SQL Server,您的 SQL Server 版本是多少?
  • "...效果不佳..." - 此信息并没有真正的帮助。它没有说明究竟是什么不起作用,即您是否在任何地方收到任何错误消息,您是否尝试调试问题以确保它确实完成了预期的事情。

标签: php mysql sql-server


【解决方案1】:

SQL Server 中 MySQL LIMIT 的大致等价物是 TOP,因此您可以尝试类似:

SELECT TOP 10 id, firstname, lastname, address, email
FROM myguests
ORDER BY <some_column>;

请注意,我在您的查询中添加了 ORDER BY 子句。在没有ORDER BY 的情况下使用LIMIT(或TOP)是相当没有意义的,因为您没有告诉SQL相对于某些排序你想要哪10 行。

【讨论】:

  • 谢谢。我认为还有其他代码需要更改。
  • @SadiMishal 您需要停止使用 MySQL 特定的方法。切换到 PHP 中的 SQL Server 库
猜你喜欢
  • 1970-01-01
  • 2012-09-14
  • 2014-09-17
  • 1970-01-01
  • 1970-01-01
  • 2013-02-22
  • 2021-12-21
  • 2020-03-09
  • 1970-01-01
相关资源
最近更新 更多