1
<?php   

$sql = "SELECT * FROM team;";
$result = mysqli_query($conn, $sql );

$row = mysqli_fetch_row($result);
$teamname = $row[1];

echo $teamname;

Consider table in DB consists of 10 names from 1-10. That will be displayed on the screen as usually (using retrieving), but the problem is when I click any name from 1-10 displaying on the screen that should open a new page by displaying only the particular name I selected.

I tried this but, all the values are displaying.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • If you expect more than one row, you need to loop over `mysqli_fetch_rows()`. – Qirel Jul 25 '19 at 08:58
  • where is your html? – devpro Jul 25 '19 at 08:58
  • @Arjun if you click on your name then you need to send you `id` to tell DB to get only your name, here in your query there is nothing like `where id = yourID` – M.Hemant Jul 25 '19 at 09:01
  • "detail" pages generally have a id which is feeded into a url... `team.php?team_id=1` catch the team_id with `$_GET['team_id']` and use it in the query something like `SELECT * FROM team WHERE team.id = $_GET['team_id']` but keep in mind the [SQL injections](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) **very important** – Raymond Nijland Jul 25 '19 at 09:05
  • can we use * there in spite of giving condition @RaymondNijland ? – Arjun Kudikala Jul 27 '19 at 16:32

1 Answers1

1

First, add to your mysql table a column called "ID" to associate a number to each name.

After you can direct the user to your page like this

<a href="www.example.com/page.php?id=1">Name n.1</a> // etc..

so you can take the id of your name using GET and display to your page only that name

(Always use Prepared Statement even when it is not strictly necessary)

PHP script

$name_id = $_GET['id'];

// connect to your db

$stmt = $db->prepare("SELECT * FROM team WHERE ID = ?");
$stmt->bind_param("s",$name_id);
$stmt->execute();
$result = $stmt->get_result()->fetch_assoc();
 echo $result['name']; 

if you need anything else, comment below.

mignaway
  • 124
  • 1
  • 9