繁体   English   中英

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

[英]Change code from mysql to mssql in a php file

我是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子句。 在没有ORDER BY情况下使用LIMIT (或TOP )是毫无意义的,因为相对于某些排序,您没有告诉SQL您想要 10行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM