简体   繁体   中英

How to echo other field (column) value with the help of id value from mysql table

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 .

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM