简体   繁体   中英

Change code from mysql to mssql in a php file

I'm new to php and sql.

The codes below are written in php and working fine when connecting to a mysql database. SELECT query is working and UPDATE query is working.

But when connecting to an mssql database, the codes don't work well. I need to convert them to connect to a similar mssql database. Thank you.


 <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));     
              }
              }

The rough equivalent of MySQL LIMIT in SQL Server is TOP , so you may try something like:

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

Note carefully that I added an ORDER BY clause to your query. Using LIMIT (or TOP ) without ORDER BY is fairly meaningless, because you haven't told SQL which 10 rows you want, relative to some ordering.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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