session_start(); $mysql_conf = array( 'host' =>'127.0.0.1:3306', 'db' =>'Student', 'db_user'=>'root', 'db_pwd' =>'123456', ); $mysqli=new mysqli($mysql_conf['host'],$mysql_conf['db_user'],$mysql_conf['db_pwd']); if($mysqli->connect_errno){ die("could not connect to the database:\n" . $mysqli->connect_errno);//诊断连接错误 } $mysqli->query("set names 'utf8';");//编码转换 $select_db = $mysqli->select_db($mysql_conf['db']); if(!$select_db){ die("could not connect to the db:/n" . $mysql->error); } $id = $_POST['id']; $sql = "select id,name from StuInfo where id='".$id."'"; if($mysqli->query($sql)){ }else{ }
时间: 2023-08-15 17:07:32 浏览: 353
zfs_iscsi:在引导 FreeBSD => 10 时为 iscsi 磁盘挂载 zfs
根据您提供的代码,缺少了对表单提交的处理和对搜索结果的显示。下面是一个完整的示例,包括表单、搜索处理和结果显示:
```php
<?php
session_start();
$mysql_conf = array(
'host' => '127.0.0.1:3306',
'db' => 'Student',
'db_user' => 'root',
'db_pwd' => '123456',
);
$mysqli = new mysqli($mysql_conf['host'], $mysql_conf['db_user'], $mysql_conf['db_pwd']);
if ($mysqli->connect_errno) {
die("could not connect to the database:\n" . $mysqli->connect_errno);//诊断连接错误
}
$mysqli->query("set names 'utf8';");//编码转换
$select_db = $mysqli->select_db($mysql_conf['db']);
if (!$select_db) {
die("could not connect to the db:/n" . $mysql->error);
}
if (isset($_POST['submit'])) {
$id = $_POST['id'];
$sql = "SELECT id, name FROM StuInfo WHERE id = '$id'";
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
echo '<table><tr><th>ID</th><th>Name</th></tr>';
while ($row = $result->fetch_assoc()) {
echo '<tr><td>' . $row['id'] . '</td><td>' . $row['name'] . '</td></tr>';
}
echo '</table>';
} else {
echo 'No results found.';
}
}
?>
<form method="POST">
<label for="id">ID:</label>
<input type="text" name="id" id="id">
<input type="submit" name="submit" value="Search">
</form>
```
该示例中,首先检查表单是否已经提交,如果是,则从表单中获取ID值,并执行查询操作。如果查询结果不为空,则使用表格显示结果;否则,显示“没有找到结果”的消息。
请注意,该示例仅用于参考目的,并且可能需要进行适当修改,以适应您的具体要求。
阅读全文