<?php$mysqli = new mysqli("localhost", "username", "password", "database");if ($mysqli -> connect_errno) { echo "Failed to connect to MySQL: " . $mysqli -> connect_error; exit();}$sql = "SELECT name, age FROM my_table";$result = $mysqli -> query($sql);if ($result -> num_rows > 0) { while ($row = $result -> fetch_assoc()) { echo "Name: " . $row["name"] . " - Age: " . $row["age"] . "<br>"; }} else { echo "0 results";}$mysqli -> close();?>
时间: 2023-07-19 21:41:37 浏览: 135
这段代码使用了mysqli扩展来连接MySQL数据库并执行SELECT语句。如果您想使用原始的MySQL写法,可以使用mysql_connect()函数来连接MySQL数据库,使用mysql_query()函数来执行SELECT语句。
以下是使用原始的MySQL写法重写上述代码的示例:
```
<?php
$link = mysql_connect("localhost", "username", "password");
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database");
$sql = "SELECT name, age FROM my_table";
$result = mysql_query($sql);
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
echo "Name: " . $row["name"] . " - Age: " . $row["age"] . "<br>";
}
} else {
echo "0 results";
}
mysql_close($link);
?>
```
请注意,mysql_connect()和mysql_query()函数已经过时,不推荐使用。为了更好的性能和安全性,建议使用mysqli或PDO扩展。
相关问题
<?php //连接数据库 $mysqli = new mysqli("localhost", "root", "root123456", "hrms"); if ($mysqli->connect_errno) { die("连接失败: " . $mysqli->connect_error); } //查询日程 $result = $mysqli->query("SELECT * FROM event"); $events = array(); while ($row = $result->fetch_assoc()) { $event = array(); $event['id'] = $row['id']; $event['title'] = $row['title']; $event['start'] = $row['start']; $event['end'] = $row['end']; $events[] = $event; } //返回JSON格式数据 echo json_encode($events); ?><?php //连接数据库 $mysqli = new mysqli("localhost", "root", "root123456", "hrms"); if ($mysqli->connect_errno) { die("连接失败: " . $mysqli->connect_error); } //查询日程 $result = $mysqli->query("SELECT * FROM event"); $events = array(); while ($row = $result->fetch_assoc()) { $event = array(); $event['id'] = $row['id']; $event['title'] = $row['title']; $event['start'] = $row['start']; $event['end'] = $row['end']; $events[] = $event; } //返回JSON格式数据 echo json_encode($events); ?>修改代码块,使day试图下的日程显示在对应的时间栏里
可以通过修改循环中构造 $event 数组的代码,在其中加入判断逻辑,根据 $row['start'] 和 $row['end'] 的值,确定该日程的开始时间和结束时间,进而确定该日程应该显示在哪个时间栏里。以下是一个可能的修改方案:
```php
//查询日程
$result = $mysqli->query("SELECT * FROM event");
$events = array();
while ($row = $result->fetch_assoc()) {
$event = array();
$event['id'] = $row['id'];
$event['title'] = $row['title'];
$event['start'] = $row['start'];
$event['end'] = $row['end'];
//根据开始时间和结束时间确定时间栏的索引
$start_time = strtotime($row['start']);
$end_time = strtotime($row['end']);
$start_hour = date('G', $start_time);
$end_hour = date('G', $end_time);
$col_index = $start_hour - 8; //假设日程表的起始时间是8点
if ($start_hour == $end_hour) {
//日程开始和结束时间在同一时间栏
$event['start'] = date('Y-m-d H:i:s', mktime(8, $start_hour * 30, 0, date('n', $start_time), date('j', $start_time), date('Y', $start_time)));
$event['end'] = date('Y-m-d H:i:s', mktime(8, $end_hour * 30 + 30, 0, date('n', $end_time), date('j', $end_time), date('Y', $end_time)));
} else {
//日程跨越多个时间栏
$event['start'] = date('Y-m-d H:i:s', mktime(8, $start_hour * 30, 0, date('n', $start_time), date('j', $start_time), date('Y', $start_time)));
$event['end'] = date('Y-m-d H:i:s', mktime(8, $end_hour * 30, 0, date('n', $end_time), date('j', $end_time), date('Y', $end_time)));
}
$events[$col_index][] = $event;
}
//返回JSON格式数据
echo json_encode($events);
```
这个方案假设日程表从早上8点开始,每个时间栏表示30分钟,因此日程开始时的时间栏索引需要减去8。如果日程开始和结束时间在同一个时间栏内,直接使用 $row['start'] 和 $row['end'] 的值即可;如果日程跨越多个时间栏,需要对 $row['start'] 和 $row['end'] 的时间戳进行处理,构造新的开始和结束时间。最后,将每个日程添加到对应的时间栏中。
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{ }
根据您提供的代码,缺少了对表单提交的处理和对搜索结果的显示。下面是一个完整的示例,包括表单、搜索处理和结果显示:
```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值,并执行查询操作。如果查询结果不为空,则使用表格显示结果;否则,显示“没有找到结果”的消息。
请注意,该示例仅用于参考目的,并且可能需要进行适当修改,以适应您的具体要求。
阅读全文