首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将php文件中的代码从mysql更改为mssql

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

Stack Overflow用户
提问于 2019-01-31 05:37:08
回答 1查看 135关注 0票数 0

我对php和sql很陌生。

下面的代码是用php编写的,连接到mysql数据库时运行良好。选择查询正在工作,更新查询正在工作。

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

代码语言:javascript
复制
 <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>  
代码语言:javascript
复制
<?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();
  }
 ?>
代码语言:javascript
复制
   <?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));     
              }
              }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-31 05:39:31

Server中MySQL LIMIT的大致等价物是TOP,因此您可以尝试如下所示:

代码语言:javascript
复制
SELECT TOP 10 id, firstname, lastname, address, email
FROM myguests
ORDER BY <some_column>;

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

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

https://stackoverflow.com/questions/54453968

复制
相关文章

相似问题

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