我是 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/