php - 将 php 文件中的代码从 mysql 更改为 mssql

标签 php mysql sql-server

我是 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 中的 MySQL LIMIT 大致相当于 TOP,因此您可以尝试如下操作:

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

请仔细注意,我在您的查询中添加了一个 ORDER BY 子句。使用LIMIT(或TOP)而不使用ORDER BY是相当没有意义的,因为你还没有告诉SQL which 10您想要的行,相对于某些顺序。

关于php - 将 php 文件中的代码从 mysql 更改为 mssql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54453968/

相关文章:

php - 使用文件目录显示数据库中的图像

php - session 销毁

php - 用 PHP 中下载的文件替换本地文件

mysql - 在 MySQL 中根据 YEAR、WEEK 和 WEEKDAY 计算 DATE

php - 显示数据库中存储的图像

java - SQL 异常,未请求生成的键

php - 数据库登录失败,Mysql、html、php

mysql - 如果主表有 2 个主键,如何使用两个表增强子查询以使用 3 个表进行搜索?

SQL基于多个子表行更新父表字段

sql - 行数据到列