首页 > 解决方案 > 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>";
?>

任何帮助,将不胜感激..

标签: phpmysql

解决方案


你可以这样做

添加

$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>";

推荐阅读