php - PHP通过ID从Mysql中获取行数据
问题描述
我是 php 新手。我有以下代码自动从数据库中获取所有行和列。我想让脚本使用其 ID 列获取特定行。例如:www.site.com/view.php ?id=22
我试图让它与这样的$_GET['link'];
变量一起工作:
if (isset($_GET['id'])) {
$result = mysqli_query($connection,"SELECT * FROM $_GET['link']");
} else {
$result = mysqli_query($connection,"SELECT * FROM reservations");
}
但我无法让它工作。
完整代码如下:
<?php
$host = "localhost";
$user = "user";
$pass = "Pass1";
$db_name = "test";
//create connection
$connection = mysqli_connect($host, $user, $pass, $db_name);
//test if connection failed
if(mysqli_connect_errno()){
die("connection failed: "
. mysqli_connect_error()
. " (" . mysqli_connect_errno()
. ")");
}
//get results from database
$result = mysqli_query($connection,"SELECT * FROM reservations");
$all_property = array(); //declare an array for saving property
//showing property
echo '<table class="data-table" border="1">
<tr class="data-heading">'; //initialize table tag
while ($property = mysqli_fetch_field($result)) {
echo '<td>' . $property->name . '</td>'; //get field name for header
array_push($all_property, $property->name); //save those to array
}
echo '</tr>'; //end tr tag
//showing all data
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
foreach ($all_property as $item) {
echo '<td>' . $row[$item] . '</td>'; //get items using property value
}
echo '</tr>';
}
echo "</table>";
?>
任何帮助,将不胜感激..
解决方案
你可以这样做
添加
$query = 'SELECT * FROM reservations';
if (!empty($_GET['id']) and ($id = (int)$_GET['id']))
$query .= " WHERE id = {$id} LIMIT 1";
并改变这个
mysqli_query($connection,"SELECT * FROM reservations");
对此
$result = mysqli_query($connection, $query);
在上面的代码中,我添加了一些安全性,以便如果$_GET['id']
它不是一个有效的整数,它将恢复到查询它获取所有数据的位置。我补充说,因为你不应该$_GET
直接输入你的查询。
这是您的代码,我已根据您的要求对其进行了修改
<?php
$host = "localhost";
$user = "user";
$pass = "Pass1";
$db_name = "test";
//create connection
$connection = mysqli_connect($host, $user, $pass, $db_name);
//test if connection failed
if(mysqli_connect_errno()){
die("connection failed: "
. mysqli_connect_error()
. " (" . mysqli_connect_errno()
. ")");
}
$query = 'SELECT * FROM reservations';
if (!empty($_GET['id']) and ($id = (int)$_GET['id']))
$query .= " WHERE id = {$id} LIMIT 1";
//get results from database
$result = mysqli_query($connection, $query);
$all_property = array(); //declare an array for saving property
//showing property
echo '<table class="data-table" border="1">
<tr class="data-heading">'; //initialize table tag
while ($property = mysqli_fetch_field($result)) {
echo '<td>' . $property->name . '</td>'; //get field name for header
array_push($all_property, $property->name); //save those to array
}
echo '</tr>'; //end tr tag
//showing all data
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
foreach ($all_property as $item) {
echo '<td>' . $row[$item] . '</td>'; //get items using property value
}
echo '</tr>';
}
echo "</table>";
推荐阅读
- concurrency - 每个队列在 spring-rabbitmq 中动态创建容器
- python - 将嵌套列表的每个元素写入 CSV 的单独列
- mongodb - 如何使用 loopback4 在 MonogoDB 数据库中创建用户
- python - math.sqrt 导致 TypeError:'float' 对象不能被解释为整数
- android - 如何在提示通知中隐藏操作按钮
- java - 如何使用带有IP地址的rest客户端连接到elasticsearch服务器
- python - 向 xarray.DataArray 添加新坐标
- clojure - 如何以同步方式迭代 Clojure 中两个不同列表中的项目?
- python - 如何从python中另一列(连续数据)的范围数据中提取一列的平均值、最大值和最小值
- c# - 带有名称的元组数组 - 可能吗?