0

Here is my Original table

enter image description here

When i Run my following PHP file, i can get below table as Result

<?php
// Create connection
$con=mysqli_connect("localhost","root","root");
//connect Database
mysql_select_db("test") or die( "Unable to select database");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$query = "SELECT no, COUNT(name) AS num FROM mytbl
          GROUP BY number";

$result=mysql_query($query) or die (mysql_error());

while($row=mysql_fetch_array($result))
{
echo "<a href='test_detail.php'>" . $row['no'] . "</a>" . "-----> " . $row['num'];
echo "<br>";
}
//connection close
mysqli_close($con);
?>  

enter image description here

When i click the links on abve table, i want to show data that related to the link as shown on below. pls help me to solve my problem.. THANK YOU

enter image description here

1
  • 1
    Pass the id with GET (or POST, although GET is better here). So link to test_detail.php?id={$row['no']} Commented Feb 8, 2014 at 9:54

3 Answers 3

1

First of all you should adjust the url to take into account the no column

echo "<a href='test_detail.php?id=".$row['no']."'>" . $row['no'] . "</a>" . "-----> " . $row['num'];

You then need to take the parameter from the url via $_GET. This can either be on the same page or you can have it on a different page. You didn't specify if the current page is test_detail.php or not.

When you take the parameter, you can insert that back into the query to get the results you desire:

$query = "SELECT * FROM mytbl WHERE no = " . $_GET["id"];

(I am not sure if the column is no or if it is number. You changed the value twice)

Please note: I have not written the code in a secure way for you. You will want to escape all data coming from the URL and assume it is insecure. You may wish to also perform other validation tests on the data to make sure it is the right datatype and within the right range (if possible).

Sign up to request clarification or add additional context in comments.

Comments

1

Pass your id to another page like this

<?php 
echo "<a href='test_detail.php?id=".$row['no']."'>" . $row['no'] . "</a>" . "-----> " . $row['num'];
?>

Now in your test_detail.php page

    // you can use your $id & fetch values according to that...
?>        

Comments

0

Beware SQL injection in other answers! http://en.wikipedia.org/wiki/SQL_injection Especially in this line $query = "SELECT * FROM mytbl WHERE no = " . $_GET["id"];

Try use http://www.php.net/manual/en/book.pdo.php instead of old and dangerous mysql_query!

Comments

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.