【发布时间】:2019-06-24 12:34:05
【问题描述】:
我是 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,您的 SQL Server 版本是多少?
-
"...效果不佳..." - 此信息并没有真正的帮助。它没有说明究竟是什么不起作用,即您是否在任何地方收到任何错误消息,您是否尝试调试问题以确保它确实完成了预期的事情。
标签: php mysql sql-server