0

我一直在尝试弄清楚如何根据 ID 显示数据库中特定行的数据。

假设我希望链接为survivaloperations.net/page/mso?p=contracts&id=#

其中 id=# 是我从数据库中提取数据的行的 ID

如何使用如上所示的链接从数据库中提取和显示数据?

我试图用谷歌搜索它,但真的不知道用谷歌搜索什么来找到相关的东西

任何帮助或参考链接表示赞赏!

这是我尝试过的:

<?php
if ($p == contracts) {
    $id = isset($_GET['id']) ? (int)$_GET['id'] : 0; // if $_GET['id'] exists, return it as an integer, otherwise use a sentinel, id's usually start with 1, so 0 works

if ($id != 0):
    // I assume this is a specific news item meaning you know it's ONE result
    $query = 'SELECT * FROM contracts WHERE id=' . $id . ' LIMIT 1'; // so try to use limit 1, no need to add extra steps in the database lookup
endif;

mysql_select_db('survival_contracts');
$result = mysql_query($query);
//$result = mysql_query($query) or die(mysql_error());
// now loop through the results
while ($row = mysql_fetch_array($result)) {
    // and use'em however you wish
    echo("<div class='mso_body_wrap'>
            <div id='mso_news_container'>
                <div class='mso_news_wrap'>
                    <div class='mso_news_top'>$row2[contract_type]</div>
                    <div class='mso_news_poster'>
                        <div class='mso_poster_avatar'><img src='images/tank.jpg'></div>
                        <div class='mso_poster_info'>for <a
                                href='#'>$row2[unit]</a><br/>by: <a
                                href='http://www.survivaloperations.net/user/$row2[userid]-$row2[username]/'>$row2[username]</a>
                        </div>
                    </div>
                    <div class='mso_news_content'>
                        <div class='mso_news_body'>
                            Callsign: $row2[callsign]<br/>
                            Urgency: $row2[urgency]<br/>
                            Location: $row2[location]<br/>
                            Grid: $row2[grid]<br/>
                            Enemy Activity: $row2[enemy_activity]<br/>
                            Hours Since Lasrt Contact: $row2[contact_hours]<br/><br/>
                            Supplies Requested: $row2[supplies]<br/>
                            Comments: $row2[comments]
                        </div>
                    </div>
                    <div class='mso_news_bottom'></div>
                </div>
            </div>");
}
?>
4

4 回答 4

1

我用我的原始代码弄清楚了:

if ($p == contracts)
        {           
            $cid = $_GET['id']; // if $_GET['id'] exists, return it as an integer, otherwise use a sentinel, id's usually start with 1, so 0 works

                $query = 'SELECT * FROM contracts WHERE id='. $cid .' LIMIT 1'; // so try to use limit 1, no need to add extra steps in the database lookup

            mysql_select_db('survival_contracts');
            $result = mysql_query($query);
            //$result = mysql_query($query) or die(mysql_error());
            // now loop through the results
            while($row = mysql_fetch_array($result)){
                // and use'em however you wish
                echo ("<div class='mso_body_wrap'>
        <div id='mso_news_container'>
            <div class='mso_news_wrap'>
                <div class='mso_news_top'>$row[contract_type]</div>
                <div class='mso_news_poster'>
                    <div class='mso_poster_avatar'><img src='images/tank.jpg'></div>
                    <div class='mso_poster_info'>for <a href='#'>$row[unit]</a><br />by: <a href='http://www.survivaloperations.net/user/$row[userid]-$row[username]/'>$row[username]</a></div>
                </div>
                <div class='mso_news_content'>
                    <div class='mso_news_body'>
                    Callsign: $row[callsign]<br />
                    Urgency: $row[urgency]<br />
                    Location: $row[location]<br />
                    Grid: $row[grid]<br />
                    Enemy Activity: $row[enemy_activity]<br />
                    Hours Since Lasrt Contact: $row[contact_hours]<br /><br />
                    Supplies Requested: $row[supplies]<br />
                    Comments: $row[comments]
                    </div>
                </div>
                <div class='mso_news_bottom'></div>
            </div>
        </div>");
            }
于 2013-08-24T00:11:03.367 回答
0

在 PHP 中搜索 $_GET 变量并查看使用 PDO 或 mysqli 的数据库连接

http://php.net/manual/en/mysqli.query.php

http://php.net/manual/en/pdo.query.php

添加代码后:

mysql_* 已弃用。尝试切换到 mysqli 或 PDO 并查看上面的链接。

于 2013-08-23T23:53:24.737 回答
0
$id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT) ? abs( (int) $_GET['id']) : 0;
if($id == 0) {
 echo 'Invalid ID';
 return;
} else {
 $query = "SELECT * FROM `table` WHERE `id`=". $id;
 $get = $db->prepare($query);
 if($get) { 
   $get = $db->query($query);
   $r = $get->fetch_array(MYSQLI_ASSOC);
   var_dump($r);
 } else {
   echo 'Could not connect to the database';
   return;
}

我混合了两种风格的 MySQLi,这不是真正的标准,但对于这个例子来说应该足够了。

http://php.net/mysqli

(确保您有数据库连接)

于 2013-08-23T23:55:21.203 回答
0

$row2 应该是 $row

像这样的东西

$row[contract_type]

最好是

$row['contract_type']

并尝试按照早期海报的建议转移到 mysqli 或 PDO

于 2013-08-24T00:37:00.810 回答