Chamal
Chamal

Reputation: 1127

How use variables in another PHP file

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

Upvotes: 0

Views: 65

Answers (3)

Stanislav Komanec
Stanislav Komanec

Reputation: 49

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!

Upvotes: 0

Dev
Dev

Reputation: 479

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

Upvotes: 1

James
James

Reputation: 2033

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

Upvotes: 1

Related Questions