I need help with Php and Mysql

Asked

Viewed 78 times

-1

I am creating a site/blog in Php, I have already made a search system in php and mysql, but I am with a doubt, I have researched on the subject and I do not think, I would like to know, how to return a query in the database links, for example, I put a field for the user to do a search, and the results come in the form of links, for example post titles, I don’t want anyone to do the code for me, just tell me how I can do it.

  • So can you describe better how your database is organized? From what I understand you want to do a query and return in "link forms", so you first need a query to capture the database data and within your database must have a column in some table that represents these links, be it a relative or absolute address. If you describe your database better, I can help you better...

  • Place the part of the code, where is the loop that returns the data in text.

1 answer

4


In a column of a table in the database, save the urls.

In the echo query put it as attribute value href tag a

echo ("<a href='".$row['link']."'>

Full example

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

//$sql = "Sua Query";
$sql = "SELECT * FROM Tabela"; //exemplo
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {

echo ("<a href='".$row['link']."'>".$row['titulo']."</a><br>\n");

    }
} else {
    echo "0 results";
}
$conn->close();

Example:

  • Table

    tabela

  • Upshot

    resultado da consulta

  • Source code

    codigo fonte

Browser other questions tagged

You are not signed in. Login or sign up in order to post.