如何将链接放入<a href=""> tag from database

时间:2017-11-07 13:29:19

标签: php

So i am building this search engine, and i have some websites inside my database. But i want to do like so i can "href" to the link i have placed inside the database. This is my code i was trying to use but didn't work:

if($query->num_rows) {
        while($r = $query->fetch_object()) {
            $rlink = $r->link;
            ?>

                <div class="result">
                    <?php echo '<a href="' .$r->link. '">' echo $r->title . "</a>"?>
                </div>
            <?php
        }
    }

And this is the error:

Parse error: syntax error, unexpected 'echo' (T_ECHO), expecting ',' or ';' in X:\xamppUSE\htdocs\search\search.php on line 33

5 个答案:

答案 0 :(得分:3)

您的语法不正确。

<?php echo '<a href="' .$r->link. '">' . $r->title . '</a>' ?>
                                      ^^^   

您无法在echo中使用echo,因此您必须连接变量

答案 1 :(得分:0)

echoquery

中删除concat with .

 <?php echo '<a href="' .$r->link. '">'.$r->title."</a>"?>

因为您已经编写了link between <?php echo ?>代码,因此无法使用echo inside echo而使用. concat operator代替echo,因此它可以正常运行。

答案 2 :(得分:0)

您在两次;来电之间错过了echo

<?php echo '<a href="' .$r->link. '">'; echo $r->title . "</a>"; ?>

但正如Stony已经建议的那样,最好只在这里使用一次echo电话。

答案 3 :(得分:-1)

你有一个额外的'回声'

替换

<?php echo '<a href="' .$r->link. '">' echo $r->title . "</a>"?>

<?php echo '<a href="' .$r->link. '">'.$r->title.'</a>'; ?>

答案 4 :(得分:-1)

这一行:

<?php echo '<a href="' .$r->link. '">' echo $r->title . "</a>"?>

你可以写成:

<a href="<?=$r->link?>"><?=$r->title?></a>

更多信息:http://php.net/manual/en/language.basic-syntax.phptags.php