Meta
Meta

Reputation: 1860

Display data for specific ID

I have been trying for a while to figure out how to display data from a specific row within my database based on the ID.

Say I want the link to be survivaloperations.net/page/mso?p=contracts&id=#

where id=# is the ID of the row I am pulling data from in the database

How would I pull and display data from the database using a link like shown above?

I Tried to google it, but didn't really know what to google to find related things

Any help or links for references are appreciated!

Here is what I had tried:

<?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>");
}
?>

Upvotes: 0

Views: 3775

Answers (4)

Aaron Gong
Aaron Gong

Reputation: 1005

$row2 should be $row

And things like

$row[contract_type]

Better to be

$row['contract_type']

And try to move to mysqli or PDO as advised by earlier poster

Upvotes: 0

Meta
Meta

Reputation: 1860

I figured it out with my original code:

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>");
            }

Upvotes: 1

sniko
sniko

Reputation: 71

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

I've mixed two styles of MySQLi, which isn't really standard, but it should suffice for this example.

http://php.net/mysqli

(Ensure you have database connection)

Upvotes: 0

Sujit Poudel
Sujit Poudel

Reputation: 541

Google for $_GET variable in PHP and have a look at database connection using PDO or mysqli

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

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

After you added code:

mysql_* is deprecated. Try to switch to either mysqli or PDO and have a look at the link above.

Upvotes: 0

Related Questions