Cody Coderson
Cody Coderson

Reputation: 421

PHP Fetch row data from Mysql via ID

I am new to php. I have the following code that auto fetches all the rows and columns from the db. I want to make the script to fetch a particular row using its ID column. for example: www.site.com/view.php?id=22

I am trying to get it work with the $_GET['link']; variable like this:

if (isset($_GET['id'])) {
    $result = mysqli_query($connection,"SELECT * FROM $_GET['link']");
} else {
    $result = mysqli_query($connection,"SELECT * FROM reservations");
}

But I am unable to get it work.

The complete code is as below:

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

Any help would be appreciated..

Upvotes: 1

Views: 3185

Answers (2)

Miroslav
Miroslav

Reputation: 336

You can do this

Add

$query = 'SELECT * FROM reservations';

if (!empty($_GET['id']) and ($id = (int)$_GET['id']))
    $query .= " WHERE id = {$id} LIMIT 1";

and change this

mysqli_query($connection,"SELECT * FROM reservations");

to this

$result = mysqli_query($connection, $query);

In the above code I added a bit of security so that if $_GET['id'] is not a valid integer it will revert to query where it fetches all the data. I added that because you should never put $_GET directly into your query.


Here is a your code I have modified it to your requirements

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

Upvotes: 3

rosy_acube
rosy_acube

Reputation: 31

if(isset($_GET['id'])) {
   $id = $_GET['id'];
} else {
  $id=NULL;
}

$sql = "SELECT * FROM reservations WHERE id = '".$id."'";
$result = mysqli_query($connection,$sql);
$row = mysqli_fetch_assoc($result);
if(mysqli_num_rows($result) == 1) {
  dd($row);
} else {
  echo "no records found with this id";
}

Hope this script meets your answer

Upvotes: 0

Related Questions