简体   繁体   中英

Searching from MySql database and display the results as a hyperlink

Here is my code, what I want to do is display the first names of my friends in another page as Hyperlinks and when a link is clicked I want to display the data.

<?php
$con=mysqli_connect("localhost","root","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM friends");



while($row = mysqli_fetch_array($result))
{

echo "<p>" . $row['FirstName'] . "</p>";
echo "<p>" . $row['LastName'] . "</p>";
echo "<p>" . $row['Email'] . "</p>";
echo "<p>" . $row['Address'] . "</p>";
}

mysqli_close($con);
?>
<?php
$con=mysqli_connect("localhost","root","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM friends");



while($row = mysqli_fetch_array($result))
{?>php

<a href=<?php "echo "<p>" . $row['FirstName'] . "</p>";"?>> `your page name`</a><?php
echo "<p>" . $row['LastName'] . "</p>";
echo "<p>" . $row['Email'] . "</p>";
echo "<p>" . $row['Address'] . "</p>";
}

mysqli_close($con);
?>

Your file from questions change:

while($row = mysqli_fetch_array($result))
{

echo "<p>" . $row['FirstName'] . "</p>";
echo "<p>" . $row['LastName'] . "</p>";
echo "<p>" . $row['Email'] . "</p>";
echo "<p>" . $row['Address'] . "</p>";
}

to

while($row = mysqli_fetch_array($result))
{
echo '<a href="show.php?UserId='.$row['UserId'].'"> . $row['FirstName'] . "</a>";
}

show.php

$con=mysqli_connect("localhost","root","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_GET['UserId'])){
if(is_numeric($_GET['UserId'])){
$UserId = (int)$_GET['UserId'];

$result = mysqli_query($con,"SELECT * FROM friends WHERE UserId= $UserId");
if(count($result)){
while($row = mysqli_fetch_array($result))
{

echo "<p>" . $row['FirstName'] . "</p>";
echo "<p>" . $row['LastName'] . "</p>";
echo "<p>" . $row['Email'] . "</p>";
echo "<p>" . $row['Address'] . "</p>";
}}}}

Important: I'm not sure what is UserId column name, so change it.

Also, please check for syntax errors.

Try connection it with an incremental ID out of the database. So every row in your friends table must have a unique ID. In that way you can connect it and get the row data from your friends table.

So your friends table must look like this:

friend_id (unique id)
firstname
lastname
email
address

And your code can be something like (this is not save from any attacks):

<?php
$con = mysqli_connect("localhost","root","abc123","my_db");
if (mysqli_connect_errno()) {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

if(isset($_GET['friendid'])) { 
    $result = mysqli_query($con,"SELECT * FROM friends");
    while($row = mysqli_fetch_array($result)) {
       echo "<p>" . $row['FirstName'] . "</p>";
       echo "<p>" . $row['LastName'] . "</p>";
       echo "<p>" . $row['Email'] . "</p>";
       echo "<p>" . $row['Address'] . "</p>";
       echo "<p><a href=\"_YOUR_SCRIPT_URL_HERE_?friendid=".$row['friend_id']."\" target=\"_self\">Details</a></p>";
    }
} else {
   $result = mysqli_query($con,"SELECT * FROM friends WHERE friend_id = '".$_GET['friendid']."'");
   $row = mysqli_fetch_assoc($result);  
   echo "<p>" . $row['FirstName'] . "</p>";
   echo "<p>" . $row['LastName'] . "</p>";
   echo "<p>" . $row['Email'] . "</p>";
   echo "<p>" . $row['Address'] . "</p>";  
} 
mysqli_close($con);
?>

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