-1

I have PHP web pages with page ids such as following example:

www.mysite.com/page.php?id=150

www.mysite.com/page.php?id=151

www.mysite.com/page.php?id=152

and so on...

The mysql table is having two columns with field names and values, for example:

id = 150

email = test@mysite.com

I am able to retrieve page id with the help of: echo $_GET['id'];

How do I fetch the email based on the GET parameter? For example for www.mysite.com/page.php?id=150 I would like to echo the email test@mysite.com on page.php.

chris85
  • 23,846
  • 7
  • 34
  • 51
Chandrakant
  • 111
  • 2
  • 12

2 Answers2

1

Okay, from your provided code you need to address the column you want the data from.

<?php 
$id = (int)$_GET['id'];
$result = mysql_query("SELECT email FROM table_name WHERE id =$id"); 
while ($row = mysql_fetch_array($result)) { 
     echo $row['email'];
     //$emails[] = $row['email']; or if you want to use them later store them.
} 
?> 

You also should update to the PDO or mysqli drivers. mysql_ is out of date. It also can't handle prepared statements which is what you should be using. This will prevent SQL injections. I've cast the user input to an integer here to avoid the injection hole.

chris85
  • 23,846
  • 7
  • 34
  • 51
0

How do I fetch and echo related email field on page.php?

That (to me) seems like you're trying to fetch the input field, if so this could be one way:

<form method = "POST" action = "page.php">
Enter the Email:
<input type = "text" name = "email">
<input type = "hidden" name = "check">
<input type = "submit" value = "Goto Next Page">
</form>

and then on page.php:

<?php
if(isset($_POST['check'])){

echo $email = $_POST['email'];

$que = "SELECT email from tablename WHERE email = '$email'";
$res = mysqli_query($con, $que);
?>

EDIT:

Moving from from the comments, this is what you should try:

<?php 
$id = (int)$_GET['id'];
$result = mysql_query("SELECT email FROM table_name WHERE id ='$id'"); 
 while ($row = mysql_fetch_array($result)) {
 echo $row['email'];
 // write more this to fetch the required data
echo $row['Your_Next_Column_Name_Here']; 
} 
?>

Notice: This will work for the time but it is strictly advised to move to either mysqli or PDO.

DirtyBit
  • 16,613
  • 4
  • 34
  • 55
  • Email address is already in database. I need to fetch it from there. – Chandrakant Sep 25 '15 at 17:56
  • either use php or ajax to fetch it from db just go through http://www.w3schools.com/php/php_mysql_select.asp – rocky Sep 25 '15 at 17:57
  • @Chandrakant Create a PHP connection with MYSQL and shoot a query somewhat like I posted in the answer and you're good to go. – DirtyBit Sep 25 '15 at 17:58
  • Please write the PHP code to fetch it from database? – Chandrakant Sep 25 '15 at 17:58
  • No, this answer opens the OP to SQL injections. 1. http://php.net/manual/en/mysqli.quickstart.prepared-statements.php 2. http://php.net/manual/en/security.database.sql-injection.php 3. http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – chris85 Sep 25 '15 at 17:59
  • I think any mysql query which uses id to fetch and echo the relevant email address. – Chandrakant Sep 25 '15 at 18:01
  • @Chandrakant your table must have a column for `email` and you can then shoot a query to get the desired email. somewhat like: `"SELECT email FROM tablename WHERE id = '5'";` – DirtyBit Sep 25 '15 at 18:03
  • Table does have column for email. id is not fixed, it varies from page to page. I have other pages such as: www.mysite.com/page.php?id=151, www.mysite.com/page.php?id=152 and so on. – Chandrakant Sep 25 '15 at 18:05
  • @Chandrakant Not an issue, Simply use the _incoming_ one `"SELECT email FROM tablename WHERE id = '$id'";` – DirtyBit Sep 25 '15 at 18:06
  • @Chandrakant what do you want to echo? the id? `echo $id;` would do it. – DirtyBit Sep 25 '15 at 18:08
  • @HawasKaPujaari it didn't worked. I have used: – Chandrakant Sep 25 '15 at 19:00
  • @Chandrakant what did you use? And what did not work? – DirtyBit Sep 25 '15 at 19:01