1

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..

2
  • 1
    Possible duplicate of GET URL parameter in PHP Commented Aug 1, 2019 at 19:37
  • Your query string you posted provides $_GET['id'] not $_GET['link']. Use print_r($_GET) To see what you are actually getting. You should also post what you ARE seeing when this runs. Commented Aug 2, 2019 at 4:37

2 Answers 2

3

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>";
Sign up to request clarification or add additional context in comments.

2 Comments

Hello @miroslav. Thanks for your reply. I am getting "Parse error: syntax error, unexpected '=' in view1.php on line 21" error for the "$query . = " WHERE id = {$id} LIMIT 1";" line.
Replace that line with this $query .= " WHERE id = {$id} LIMIT 1"; I have also updated the code.
0
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

2 Comments

It's giving Parse error: syntax error, unexpected '"'"' (T_CONSTANT_ENCAPSED_STRING) error for the '".$id"'"
Can you please show me with complete code? Also if the .php page is opened directly, I want the first 10 results of the db to be shown as the format of the table as usual.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.