0

I have the following code that works by outputting as a link ( the link comes from a field in my database) I wish to do the same for the code below, however i cannot get it work, here is the example of what I have that works, and the code that i wish to make output as a link:

Working Code what I want it to look like

if (!empty($_REQUEST['term'])) {

$term = mysql_real_escape_string($_REQUEST['term']);     

$sql = "SELECT * FROM adrenaline WHERE title LIKE '%".$term."%'"; 
$r_query = mysql_query($sql); 

while ($row = mysql_fetch_array($r_query)){  
     echo '<br> <a href="../' . $row['description'] . '"> '. $row['title'] .'</a>';  
 }

}  

?>

And the code that i have at the moment, it works by be manually typing in the hyper link, however I wish to make it take the link from the database like the example above

//query the database
$query = mysql_query("SELECT * FROM hobby WHERE id = '1' ");

//ferch the results / convert results into an array

    WHILE($rows = mysql_fetch_array($query)):

        $title = $rows['title'];

    echo "<a href='shard.php'>$title</a>";


endwhile;
?>

Many thanks!

2
  • echo sprintf("<a href=\"%s\">%s</a>", $rows["link"], $rows["title"]); Commented Mar 17, 2014 at 18:02
  • replace $rows["link"] with whatever your link/pagename variable is. Commented Mar 17, 2014 at 18:04

1 Answer 1

1

I am not 100% certain if this is what you meant to ask... let me know in comments:

   <?PHP
    $query = mysql_query("SELECT * FROM hobby WHERE id = '1' ");
    if(mysql_num_rows($query)   >=  1) {
        while($rows =   mysql_fetch_array($query)) {
            echo sprintf("<a href=\"%s\">%s</a>", $rows["description"], $rows["title"]);
        }
    } else { echo "No hobbies found."; }
    ?>

I believe you might have faced some syntax issues while dealing with quotes parsing a variable in <a html tag. Consider using sprintf something like in my example.


I have also added a mysql_num_rows() just in case and you can see its a good fail-safe method incase there are no rews found on any select query.


IMPORTANT: STOP using mysql_ functions because its deprecated from new PHP versions. Use PDO or mysqli instead.

Sign up to request clarification or add additional context in comments.

1 Comment

If we got paid everytime we said STOP using mysql_ we would have no need for day jobs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.