0

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

chris85
  • 23,846
  • 7
  • 34
  • 51
  • 1
    why are you using echo when you just need to concatenate strings and its done? – FilipRistic Nov 07 '17 at 13:32
  • Not that I can provide anything more than the answers here, but the error tells you what's wrong. 'Unexpected 'echo'. If you look at your line of code, you start the line with echo... then you echo again without terminating the initial one. Just replace the second echo with a full stop. – Lee Nov 07 '17 at 13:34
  • Why does this need so many different answers, that all say the same thing? – Lee Nov 07 '17 at 13:34
  • @Lee people see the opportunity for a quick accepted answer. I'm bugged by the fact the only one getting upvotes has a missing semicolon, but whatever. – brianforan Nov 07 '17 at 13:38
  • That's not true. Bhargav also is missing the semi-colon. I tend to select the first answer given that actually gives decent information to solve the issue, which Stony's does. Just trying to keep SO clean and tidy of all the useless answers. – Lee Nov 07 '17 at 13:41
  • @brianforan A closing semi-colon is optional in PHP when the block is being closed. http://php.net/manual/en/language.basic-syntax.instruction-separation.php `The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block.` – chris85 Nov 07 '17 at 14:12
  • @chris85 oh interesting. i'd still use it personally, but i guess if it works then it works – brianforan Nov 07 '17 at 14:30
  • I have found out the solution... –  Nov 20 '17 at 14:29

5 Answers5

3

You syntax is not correct.

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

you can't use echo in an echo so you have to concat the variable

René Höhle
  • 26,716
  • 22
  • 73
  • 82
  • I'd drop the concatenation personally and template it to be cleaner. `link}'>{$r->title}"; ?>` - you also forgot your semicolon after your echo. – brianforan Nov 07 '17 at 13:35
0

remove echo from query and concat with .

like

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

because you already write link between <?php echo ?> tag so you can not use echo inside echo you use . concat operator instead of echo so it will work.

Bhargav Chudasama
  • 6,928
  • 5
  • 21
  • 39
0

You are missing a ; between your two echo calls.

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

But as Stony already suggested, it´s preferrable to use only one echo call here.

musashii
  • 445
  • 6
  • 13
-1

You have an extra 'echo'

Replace

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

with

<?php echo '<a href="' .$r->link. '">'.$r->title.'</a>'; ?>
sandcode
  • 36
  • 1
  • 6
-1

This line:

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

You could write as:

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

More info: http://php.net/manual/en/language.basic-syntax.phptags.php

Savado
  • 557
  • 1
  • 3
  • 18
  • Slightly confusing, and much more than is needed to fix the error. – Lee Nov 07 '17 at 13:34
  • @Lee Why the downvote? This is a personal preference. For me, this is more readable and less confusing. – Savado Nov 07 '17 at 13:35
  • I don't know about that, all I know is @Lee downvoted my answer for no good reason. – Savado Nov 07 '17 at 13:49
  • @Savado Answers should have an explanation so users know why the code works. Just saying `run this`, `use this` etc. doesn't teach the user anything and leads them to a bad practice (running code when unaware what it actually does). – chris85 Nov 07 '17 at 14:16
  • 1
    Thank you, @chris85. I'll add more explanation in future answers. – Savado Nov 07 '17 at 14:46